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
题意很简单,找某两点之间最小的角,读入,atan2()得角度排序,再遍历得最小角度差。
atan2()函数非常方便:
long double atan2(long double y, long double x) 返回角度
代码:
/*C题精度要求高需要使用long double,推荐使用atan2函数,说明如下:
C 语言里long double atan2(long double y,long double x) 返回的是原点至点(x,y)的方位角,即与 x 轴的夹角。
也可以理解为复数 x+yi 的辐角。返回值的单位为弧度,取值范围为(-PI,PI]。*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#define PI acos(-1.0)
using namespace std;
const int maxn=100000;
struct point{
long double a;
int num;
}p[maxn+10];
bool cmp(point p1,point p2)
{
return p1.a<p2.a;
}
int main()
{
int n;
int x,y;
int mina,minb;
long double mi=PI;
scanf("%d",&n);
int k=0;
for(int i=0;i<n;++i){
scanf("%d%d",&x,&y);
p[k].a=atan2(y*1.0,x*1.0);//printf("%lf\n",p[k].a);
p[k].num=i+1;
++k;
}
sort(p,p+k,cmp);
for(int i=1;i<k;++i){
if(p[i].a-p[i-1].a<=mi){
mi=p[i].a-p[i-1].a;
mina=p[i-1].num;
minb=p[i].num;//printf("%d %d %lf\n",mina,minb,mi);
}
}
if(p[0].a-p[k-1].a+2*PI<=mi){
mi=p[0].a-p[k-1].a+2*PI;
mina=p[0].num;
minb=p[k-1].num;//printf("%d %d\n",mina,minb);
}
printf("%d %d\n",mina,minb);
return 0;
}