codeforces 598C C. Nearest vectors(极角排序)
2016-03-21 23:51
435 查看
题目链接:
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
output
input
output
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 ; 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 .x; y2=point .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 .num; mmin=w; } printf("%d %d\n",ansa,ansb);
相关文章推荐
- 《网站设计 网页配色》笔记
- 学java时遇到的一些小问题
- :Hibernate逍遥游记-第16管理session和实现对话
- typedef定义函数指针和block
- 集合对象set及其操作
- 汉诺塔III
- UVA10954哈夫曼树与优先队列
- Spring源码之ApplicationContext(九)初始化剩余的单例
- Android 热补丁动态修复框架小结
- 没事画画图
- 1.1.2A+B for Input-Output Practice (II)
- Hrbust Online Judge 排列问题
- android的常用布局LinearLayout和RelativeLayout
- BZOJ4444 : [Scoi2015]国旗计划
- Codeforces 400C Inna and Huge Candy Matrix 【模拟】
- Spring源码之ApplicationContext(八)消息广播
- NYOJ-214-单调递增子序列(二)
- 基于django写的一个blog系统
- Debian下交换ctrl与esc键映射
- leetcode:Find Median from Data Stream