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 π.
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).
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.
4 -1 0 0 -1 1 0 1 1
3 4
6 -1 0 0 -1 1 0 1 1 -4 -5 -4 -6
6 5
题意:有n个向量,起点都为原点,给出终点,让你求出这些向量中夹角最小的两个向量。
思路:赛中的思路是求出所有VEC与VEC(0,1)的夹角,再排序,然后扫一遍就OK了,赛中是一发过了,但是赛后被HACK掉了,看了别人的代码发现需要用long double否则精度不够,做计算几何一定要小心精度问题。
这是被hack的数据:
4
-9901 9900
-10000 9899
9899 9801
9899 9900
ans is 3 4
~真是牛逼啊,这么极端的数据 。
注意的地方:
1.我求角度时用的是大白书上面的模板,将double换成long double后会比较慢,看了别人的代码发现求角度可以用atan(y,x)这个函数。
2.科普一下 atan2(y,x):
3.在改数组为long double 后,要记得把函数返回的参数也改成long double,否则然并卵。
下面是我改过的代码,用大白书上的模板,跑了1000ms +:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
typedef long double LD;
const LD pi=acos(-1.0);
const LD eps=1e-15;
int dcmp(LD x){
if(fabs(x)<=eps) return 0;
if(x<0) return -1;
return 1;
}
struct Point
{
LD x,y;
Point(){}
Point(double _x,double _y){
x = _x;y = _y;
}
}p[100005];
typedef Point Vec;
LD Dot (Vec A,Vec B) { return A.x*B.x+A.y*B.y; }
LD Length(Vec A) { return sqrt(Dot(A,A)); }
LD Angle(Vec A,Vec B) { return acos(Dot(A,B)/Length(A)/Length(B)); }
LD ang[100005];
int num[100005];
bool cmp(int a,int b){
return ang[a]<ang[b];
}
int main (){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
cin>>p[i].x>>p[i].y;
ang[i]=Angle(Vec(p[i].x,p[i].y),Vec(0,10000));
if(ang[i]<pi&&p[i].x<0)
ang[i]=2*pi-ang[i];
num[i]=i;
}
cout.precision(100);
//cout<<endl;
sort(num+1,num+n+1,cmp);
//for(int i=1;i<=n;i++)
// cout<<num[i]<<" "<<ang[num[i]]<<endl;
LD mid=min(fabs(ang[num[1]]-ang[num[n]]),2*pi-fabs(ang[num[1]]-ang[num[n]]));
LD minn=mid;
int ot1=num[1],ot2=num[n];
//cout<<minn<<endl;
for(int i=2;i<=n;i++){
mid=min(fabs(ang[num[i-1]]-ang[num[i]]),2*pi-fabs(ang[num[i-1]]-ang[num[i]]));
//cout<<mid<<endl;
if(minn>mid){
minn=mid;
ot1=num[i-1];
ot2=num[i];
//cout<<ot1<<" "<<ot2<<endl;
}
}
printf("%d %d\n",ot1,ot2);
}
return 0;
}
下面是这场CF rank1的兄弟的代码,简洁明了,速度飞快 63ms:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;
typedef long double LD;
const LD pi=acos(-1.0);
const LD eps=1e-20;
int dcmp(LD x){
if(fabs(x)<eps)
return 0;
else
return x<0 ? -1 : 1;
}
LD ang[100005];
int num[100005];
bool cmp(int a,int b){
return ang[a]<ang[b];
}
int main (){
cout.precision(12);
int n;
while(scanf("%d",&n)!=EOF){
int x,y;
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
ang[i]=atan2(1.0*y,1.0*x);
num[i]=i;
}
sort(num+1,num+n+1,cmp);
LD mid=min(fabs(ang[num[1]]-ang[num[n]]),2*pi-fabs(ang[num[1]]-ang[num[n]]));
LD minn=mid;
int ot1=num[1],ot2=num[n];
for(int i=2;i<=n;i++){
mid=min(fabs(ang[num[i-1]]-ang[num[i]]),2*pi-fabs(ang[num[i-1]]-ang[num[i]]));
if(minn>mid){
minn=mid;
ot1=num[i-1];
ot2=num[i];
}
}
printf("%d %d\n",ot1,ot2);
}
return 0;
}