CodeForces - 598C 极角

CodeForces - 598C 极角
题目描述:
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

题目大意:给出n个坐标,坐标两两之间可以形成角度,问形成的最小的那个角的两个坐标的编号
思路:应该注意到相邻的两个坐标找角。我们可以先把每个角距离x轴的角度算出来,这里注意什么时候要角加上90度,然后排序一下,相减去最小值,再和头尾坐标形成的角特判一下。注意利用结构体把坐标标号存好。

#include <iostream>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
int n ;
long double x,y;
long double b[200010];
struct node {
    long double angle;
    int id;
}a[200010];

const long double PI = acos(-1.0);//写1会WA

bool cmp (const node &t1, const node &t2) {
    return t1.angle < t2.angle;
}

int main(){
    while(~scanf("%d",&n)){
        for(int i = 1 ; i <= n ; ++i){
            cin >> x >> y;
            a[i].id = i;
            a[i].angle = atan2(x,y);//取值范围是(-PI,PI]
        }
        sort(a+1 , a+n+1, cmp);
        long double t = a[1].angle - a[n].angle + 2 * PI ;//头尾坐标形成的角度
        int t1 = 1 , t2 = n;
        for(int i = 2 ; i <= n ; ++i){
            long double temp = a[i].angle - a[i-1].angle;
            if(temp < t){
                t = temp;
                t1 = i;
                t2 = i -1;
            }

        }   
        printf("%d %d\n",a[t1].id,a[t2].id);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Love_Yourself_LQM/article/details/81710122