Space Ant OpenJ_Bailian - 1696

极角排序构造

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

const int maxn=600;
const double eps=1e-8;

int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    double x,y;
    int index;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};
int pos;
Point p[maxn];

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b)
{
    double ans=sgn((a-p[pos])^(b-p[pos]));
    if(ans>0)
        return true;
    else if(ans==0)
        return dist(a,p[pos])<dist(b,p[pos]);
    else
        return false;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m;
        cin>>m;
        for(int i=0;i<m;i++)
        {
            int index;
            double x1,y1;
            cin>>index>>x1>>y1;
            p[i].index=index;
            p[i].x=x1;
            p[i].y=y1;
            if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
                swap(p[0],p[i]);
        }
        printf("%d",m);
        pos=0;
        for(int i=1;i<m;i++)
        {
            sort(p+i,p+m,cmp);
            pos++;
        }
        for(int i=0;i<m;i++)
        {
            printf(" %d",p[i].index);
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/81185710