Educational Codeforces Round 1 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个向量, 求夹角最小的两个向量。

直接模拟肯定会超时, 首先对对每一个向量求其对x轴正方向的角度。

然后求其差最小的。但是还是会超时。

可以对其先排序然后求差(后一项减前一项),不要忘了还有第一项减最后一项。

对于一个题目,先要推一推找规律,不要轻易放弃。

///用long double , double会WA
#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         using namespace std; const int N = 100001; struct node { long double val; int x; }a[N]; bool cm(node a, node b) { return a.val < b.val; } const long double pi = 3.1415926535897932384626433832795028841971; int main(void) { int n, i, j; scanf("%d", &n); for(i = 0; i < n; i++) { double x, y; scanf("%lf%lf", &x, &y); a[i].val = atan2(x, y); a[i].val += pi; a[i].x = i+1; // printf("i=%d %lf\n", i, a[i].val); } sort(a, a+n, cm); node x = a[0], y = a[1]; for(i = 1; i < n-1; i++) { if(a[i+1].val - a[i].val < y.val - x.val) { x = a[i]; y = a[i+1]; } } if(a[0].val+pi*2 - a[n-1].val < y.val - x.val) { x = a[0]; y = a[n-1]; } printf("%d %d\n", x.x, y.x); return 0; } 
       
      
      
     
     
    
    
   
   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值