codeforces難度,codeforces 598C C. Nearest vectors(極角排序)

 2023-10-07 阅读 15 评论 0

摘要:題目鏈接: 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 pa

題目鏈接:

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

codeforces難度,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);

?

轉載于:https://www.cnblogs.com/zhangchengc919/p/5304396.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/125989.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息