【STL + 几何】Nearest vectors CodeForces - 598C 【求两点间极角差】long double atan2(long double y,long double x)

求原点至点(x,y)的方位角,即与 x 轴的夹角的函数
long double atan2(long double y,long double x)

【STL + 几何】Nearest vectors 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 轴] 的夹角叫做极角。求极角差最小的两个点。范围在0-pi

思路:
先处理出所有极角,然后将极角排序,求出极角差,【注意处理最后一个角和第一个角直接的差】,求出最小值。因为需要输出是哪两个点之间的差最小,所有要用map标记一下序号。

TIPS
这里精度要求高需要使用long double,推荐使用atan2函数,说明如下:
C 语言里long double atan2(long double y,long double x) 返回的是原点至点(x,y)的方位角,即与 x 轴的夹角。也可以理解为复数 x+yi 的辐角。返回值的单位为弧度,取值范围为(-PI,PI]。

AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <math.h>

using namespace std;
const int maxn = 100005;
long double ang[maxn], pi = 2 * asin(1);
map<long double, int> dx;
map<long double, int>::iterator it, pre;

long double sol(long double ax, long double ay)
{
    long double d = abs(ax - ay);
    if(d > pi)
        return 2 * pi - d;
    else
        return d;
}

int main()
{
    int n, x, y;
    while(~scanf("%d", &n))
    {
        dx.clear();
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &x, &y);
            dx.insert(make_pair(atan2(x, y), i + 1));
        }
        int xx, yy;
        long double ans = 5, cur;
        pre = --dx.end();
        for(it = dx.begin(); it != dx.end(); it++)
        {
            cur = sol(it->first, pre->first);
            if(cur < ans)
            {
                ans = cur;
                xx = it->second;
                yy = pre->second;
            }
            pre = it;
        }
        cout<<xx<<" "<<yy<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值