Codeforces Educational Round 1 C题

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个向量,起点都为原点,给出终点,让你求出这些向量中夹角最小的两个向量。

思路:赛中的思路是求出所有VEC与VEC(0,1)的夹角,再排序,然后扫一遍就OK了,赛中是一发过了,但是赛后被HACK掉了,看了别人的代码发现需要用long double否则精度不够,做计算几何一定要小心精度问题。

这是被hack的数据:

4
-9901 9900
-10000 9899
9899 9801
9899 9900

ans is    3   4
 ~真是牛逼啊,这么极端的数据 。

注意的地方:

1.我求角度时用的是大白书上面的模板,将double换成long double后会比较慢,看了别人的代码发现求角度可以用atan(y,x)这个函数。

2.科普一下  atan2(y,x):

atan结果为正表示从 X 轴逆时针旋转的角度,结果为负表示从 X 轴顺时针旋转的角度。
ATAN2(a, b) 与 ATAN(a/b)稍有不同,ATAN2(a,b)的取值范围介于 -pi 到 pi 之间(不包括 -pi),
而ATAN(a/b)的取值范围介于-pi/2到pi/2之间(不包括±pi/2)。

3.在改数组为long double 后,要记得把函数返回的参数也改成long double,否则然并卵。

下面是我改过的代码,用大白书上的模板,跑了1000ms +:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

typedef long double LD;
const LD pi=acos(-1.0);
const LD eps=1e-15;

int dcmp(LD x){
    if(fabs(x)<=eps)  return 0;
    if(x<0)  return -1;
    return 1;
}

struct Point
{
    LD x,y;
    Point(){}
    Point(double _x,double _y){
        x = _x;y = _y;
    }
}p[100005];

typedef Point Vec;

LD Dot (Vec A,Vec B)  { return A.x*B.x+A.y*B.y; }

LD Length(Vec A)  { return sqrt(Dot(A,A)); }

LD Angle(Vec A,Vec B)  { return acos(Dot(A,B)/Length(A)/Length(B)); }

LD ang[100005];
int num[100005];

bool cmp(int a,int b){
    return ang[a]<ang[b];
}

int main (){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            cin>>p[i].x>>p[i].y;
            ang[i]=Angle(Vec(p[i].x,p[i].y),Vec(0,10000));
            if(ang[i]<pi&&p[i].x<0)
                ang[i]=2*pi-ang[i];
            num[i]=i;
        }
        cout.precision(100);
        //cout<<endl;
        sort(num+1,num+n+1,cmp);
        //for(int i=1;i<=n;i++)
        //    cout<<num[i]<<"   "<<ang[num[i]]<<endl;

        LD mid=min(fabs(ang[num[1]]-ang[num[n]]),2*pi-fabs(ang[num[1]]-ang[num[n]]));
        LD minn=mid;
        int ot1=num[1],ot2=num[n];
        //cout<<minn<<endl;
        for(int i=2;i<=n;i++){
            mid=min(fabs(ang[num[i-1]]-ang[num[i]]),2*pi-fabs(ang[num[i-1]]-ang[num[i]]));
            //cout<<mid<<endl;
            if(minn>mid){
                minn=mid;
                ot1=num[i-1];
                ot2=num[i];
                //cout<<ot1<<"   "<<ot2<<endl;
            }
        }
        printf("%d %d\n",ot1,ot2);
    }
    return 0;
}

下面是这场CF rank1的兄弟的代码,简洁明了,速度飞快 63ms:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

typedef long double LD;
const LD pi=acos(-1.0);
const LD eps=1e-20;

int dcmp(LD x){
    if(fabs(x)<eps)
        return 0;
    else
        return x<0 ? -1 : 1;
}

LD ang[100005];
int num[100005];

bool cmp(int a,int b){
    return ang[a]<ang[b];
}

int main (){
    cout.precision(12);
    int n;
    while(scanf("%d",&n)!=EOF){
        int x,y;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            ang[i]=atan2(1.0*y,1.0*x);
            num[i]=i;
        }
        sort(num+1,num+n+1,cmp);
        LD mid=min(fabs(ang[num[1]]-ang[num[n]]),2*pi-fabs(ang[num[1]]-ang[num[n]]));
        LD minn=mid;
        int ot1=num[1],ot2=num[n];
        for(int i=2;i<=n;i++){
            mid=min(fabs(ang[num[i-1]]-ang[num[i]]),2*pi-fabs(ang[num[i-1]]-ang[num[i]]));
            if(minn>mid){
                minn=mid;
                ot1=num[i-1];
                ot2=num[i];
            }
        }
        printf("%d %d\n",ot1,ot2);
    }
    return 0;
}


校园失物招领微信小程序源码, 失物招领小程序主要为解决大学生时常丢失物品而且很难找回以及归还过程繁琐不方便的问, 与传统的失物招领方式不同,该款校园失误招领小程序拥有快捷发布寻物启事和失误找领功能, 快速查找、极速归还、高效沟通、防误领冒领等功能, 在开发校园失物招领小程序前与用户访谈发现有近40的同学校园内频繁丢失物品、证件、校园卡等, 数码产品、日用品等,丢失区域主要发生在教学楼、图书馆和食堂。 拾领校园失物招领小程序继承了寻物启事和失物招领,丢失物品或拾取物品都可发布帖子, 首页的横幅滚动公告展示通知公告等,banner图片化的方式更具有视觉吸引力, 最新信息可显示最近发布的招领信息或寻物信息,更加方便快捷的展示信息, 用户可通过首页的发布按钮发布帖子,发布者只需填写物品的相关信息,类别、地点等相关信息, 并且可以填写手机号开启认领验证,并可以一键生成二维码分享或分享至群聊和朋友圈。 列表内可以筛选物品类别或精确搜索,物品详情里可展示物品的相关信息, 确认是自己的物品后可点击认领,然后验证信息,需填写物品的关键信息以作辨认, 防止冒领误领,物品详情页可生成二维码海报分享,还有即时的消息联系功能以提高沟通效率, 发布者还可选择放置在代收处,双方还可以通过拨打电话紧急联系,用于紧急情况,让失物找到主人, 个人中心可以管理发布的物品帖子,管理个人信息,包括昵称、默认学校、手机号的修改、 编辑发布的物品帖子、获取帮助等。帮助用户流畅的使用该小程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值