Nearest vectors CodeForces - 598C

用 long

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

using namespace std;

const int maxn=111111;
const double eps=1e-8;
const long double PI=acos(-1.0);
int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    long double x,y;
    int index;
    Point(){}
    Point(long double _x,long double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    long double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    long double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

Point p[maxn];
Point ap=Point(0,0);
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));

}
bool cmp(Point a,Point b)
{
    int ans=sgn((a-ap)^(b-ap));
    if(ans==1)
        return true;
    else if(ans==0)
        return dist(a,ap)<dist(b,ap);
    else
        return false;
}
bool cmp1(Point a,Point b)
{
    if((long double)atan2(a.y,a.x)!=(long double)atan2(b.y,b.x))
        return (long double)atan2(a.y,a.x)<(long double)atan2(b.y,b.x);
    else return a.x<b.x;
}

int main()
{
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cin>>p[i].x>>p[i].y;
        p[i].index=i+1;
    }
    long double max1=1e18;
    int a1,a2;
    sort(p,p+n,cmp1);
    for(int i=0;i<n;i++)
    {
        long double t1=atan2(p[i].y,p[i].x)*180/PI;
        long double t2=atan2(p[(i+1)%n].y,p[(i+1)%n].x)*180/PI;
        long double ans;
        if(i==(n-1))
        {
            ans=(t1-t2);
        }
        else
        {
            ans=-(t1-t2);
        }
        if(ans>180)
            ans=360-ans;
        if(ans<max1)
        {
            max1=ans;
            a1=p[i].index;
            a2=p[(i+1)%n].index;
        }
    }
    cout<<a1<<" "<<a2<<endl;
    return 0;
}

double 提高精度

猜你喜欢

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