您的位置:首页 > 其它

Codeforces Educational Round 1 C题

2015-12-01 12:46 295 查看
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.

Sample test(s)

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


题意:有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):

atan结果为正表示从 X 轴逆时针旋转的角度,结果为负表示从 X 轴顺时针旋转的角度。

ATAN2(a, b) 与 ATAN(a/b)稍有不同,ATAN2(a,b)的取值范围介于 -pi 到 pi 之间(不包括 -pi),

而ATAN(a/b)的取值范围介于-pi/2到pi/2之间(不包括±pi/2)。

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
]),2*pi-fabs(ang[num[1]]-ang[num
]));
LD minn=mid;
int ot1=num[1],ot2=num
;
//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
]),2*pi-fabs(ang[num[1]]-ang[num
]));
LD minn=mid;
int ot1=num[1],ot2=num
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: