codeforces 598C C. Nearest vectors(极角排序)

题目链接:

C. Nearest vectors

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.

Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.

Input

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.

The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).

Output

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.

Examples
input
4
-1 0
0 -1
1 0
1 1
output
3 4
input
6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6
output
6 5

题意:找到两个向量间夹角最小的那两个向量的位置;
思路:直接暴力绝对绝对绝对会超时,所以要先按极角排序,排完后再找两个相邻的向量夹角最小的那对,一开始自己用余弦定理求角发现精度不够,看网上说用long double ,改成long double 后还是被test104和test105卡死了,所以换成atan2函数最后才过,看来余弦定理求还是精度不行;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+20;
const long double PI=acos(-1.0);
struct node
{
    int num;
    long double x,y;
    long double angle;
};
node point[N];
int cmp(node s1,node s2)
{
    return s1.angle<s2.angle;
}
int main()
{
    int n;
    double xx,yy;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        cin>>xx>>yy;
        //scanf("%lf%lf",&xx,&yy);
        point[i].x=xx;
        point[i].y=yy;
        point[i].num=i;
        point[i].angle=atan2(yy,xx);
        //point[i].angle=acos(xx/sqrt(xx*xx+yy*yy));
        //if(yy<0)point[i].angle=2*PI-point[i].angle;
    }
    sort(point+1,point+n+1,cmp);
    int ansa,ansb;
    long double mmin=90,w;
    long double x1,y1,x2,y2;
    for(int i=2;i<=n;i++)
    {

        x1=point[i].x;
        y1=point[i].y;
        x2=point[i-1].x;
        y2=point[i-1].y;
        w=atan2(y1,x1)-atan2(y2,x2);
        if(w<0)w+=2*PI;
        //acos((x1*x2+y1*y2)/(sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2)));
        if(w<=mmin)
        {
            ansa=point[i-1].num;
            ansb=point[i].num;
            mmin=w;
        }
    }
        x1=point[1].x;
        y1=point[1].y;
        x2=point[n].x;
        y2=point[n].y;
        w=atan2(y1,x1)-atan2(y2,x2);
        if(w<0)w+=2*PI;
        //w=acos((x1*x2+y1*y2)/(sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2)));
    if(w<mmin)
    {
            ansa=point[1].num;
            ansb=point[n].num;
            mmin=w;
    }
    printf("%d %d\n",ansa,ansb);

   

 

你可能感兴趣的:(codeforces 598C C. Nearest vectors(极角排序))