codeforces 598C C. Nearest vectors(极角排序)
题目链接:
2 seconds
256 megabytes
standard input
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 π.
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).
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.
4
-1 0
0 -1
1 0
1 1
3 4
6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6
6 5 题意:找到两个向量间夹角最小的那两个向量的位置;
思路:直接暴力绝对绝对绝对会超时,所以要先按极角排序,排完后再找两个相邻的向量夹角最小的那对,一开始自己用余弦定理求角发现精度不够,看网上说用long double ,改成long double 后还是被test104和test105卡死了,所以换成atan2函数最后才过,看来余弦定理求还是精度不行;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
const long double PI=acos(-1.0);
struct node
{
int num;
long double x,y;
long double angle;
};
node point[N];
int cmp(node s1,node s2)
{
return s1.angle<s2.angle;
}
int main()
{
int n;
double xx,yy;
scanf("%d",&n);
for(int i=;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+,point+n+,cmp);
int ansa,ansb;
long double mmin=,w;
long double x1,y1,x2,y2;
for(int i=;i<=n;i++)
{ x1=point[i].x;
y1=point[i].y;
x2=point[i-].x;
y2=point[i-].y;
w=atan2(y1,x1)-atan2(y2,x2);
if(w<)w+=*PI;
//acos((x1*x2+y1*y2)/(sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2)));
if(w<=mmin)
{
ansa=point[i-].num;
ansb=point[i].num;
mmin=w;
}
}
x1=point[].x;
y1=point[].y;
x2=point[n].x;
y2=point[n].y;
w=atan2(y1,x1)-atan2(y2,x2);
if(w<)w+=*PI;
//w=acos((x1*x2+y1*y2)/(sqrt(x1*x1+y1*y1)*sqrt(x2*x2+y2*y2)));
if(w<mmin)
{
ansa=point[].num;
ansb=point[n].num;
mmin=w;
}
printf("%d %d\n",ansa,ansb);
codeforces 598C C. Nearest vectors(极角排序)的更多相关文章
- Educational Codeforces Round 1 C. Nearest vectors 极角排序
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...
- C. Nearest vectors--cf598C(极角排序)
http://codeforces.com/problemset/problem/598/C 题目大意: 给你你个向量 向量的起点都是从(0,0)开始的 求哪个角最小 输出这两个向量 这是第一 ...
- CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)
传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...
- [置顶] Codeforces 70D 动态凸包 (极角排序 or 水平序)
题目链接:http://codeforces.com/problemset/problem/70/D 本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包 判断:平衡树log(n)内选出点 ...
- codeforces 1284E. New Year and Castle Construction(极角排序+扫描枚举)
链接:https://codeforces.com/problemset/problem/1284/E 题意:平面上有n个点,问你存在多少组四个点围成的四边形 严格包围某个点P的情况.不存在三点共线. ...
- Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)
C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces 196C Paint Tree(贪心+极角排序)
题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...
- HCW 19 Team Round (ICPC format) H Houston, Are You There?(极角排序)
题目链接:http://codeforces.com/gym/102279/problem/H 大致题意: 你在一个定点,你有个长度为R的钩子,有n个东西在其他点处,问你能勾到的东西的数量是多少? 思 ...
- POJ 1696 Space Ant 【极角排序】
题意:平面上有n个点,一只蚂蚁从最左下角的点出发,只能往逆时针方向走,走过的路线不能交叉,问最多能经过多少个点. 思路:每次都尽量往最外边走,每选取一个点后对剩余的点进行极角排序.(n个点必定能走完, ...
随机推荐
- Http协议 详解(转载)
http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx 引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分 ...
- github 答题
头脑王者 / 百万英雄 / 冲顶大会 / 芝士超人 自动答题:https://github.com/cxs1994/python_answer 头脑王者:https://github.com/sear ...
- 安装Hadoop 1.1.2 (一 安装JDK)
1 下载jdk1.7 xxx .rpm 2 以Root权限登陆 3 修改文件权限 chmod +x jdk-7u25-linux-x64.rpm 4 安装 JDK rpm -ivh jdk-7u2 ...
- C++中面向对象的理解
1.对于OO(面向对象)的含义,并非每一个人的看法都是同样的. 即使在如今.假设问十个人,可能会得到15种不同的答案.差点儿全部的人都会允许继承和多态是OO中的概念.大多数人还会再加上封装. 另 ...
- Miller-Rabin大素数测试模板
根据费马小定理: 对于素数n,a(0<a<n),a^(n-1)=1(mod n) 如果对于一个<n的正整数a,a^(n-1)!=1(mod n),则n必不是素数. 然后就可以随机生成 ...
- SET IDENTITY_INSERT ON/OFF 权限
今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...
- 【问题解决】Tomcat 启动时闪退或提示“Neither the JAVA_HOME or the JRE_HOME environmental variable is defined.”
问题解决思路: 1.分析startup.bat启动脚本:发现其调用了catalina.bat,而catalina.bat调用了setclasspath.bat 2.在setclasspath.bat的 ...
- 【BZOJ2741】【FOTILE模拟赛】L 分块+可持久化Trie树
[BZOJ2741][FOTILE模拟赛]L Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max( ...
- 【BZOJ2790】[Poi2012]Distance 筛素数+调和级数
[BZOJ2790][Poi2012]Distance Description 对于两个正整数a.b,这样定义函数d(a,b):每次操作可以选择一个质数p,将a变成a*p或a/p, 如果选择变成a/p ...
- python脚本分析nginx访问日志
日志格式如下: 223.74.135.248 [11/May/2017:11:19:47 +0800] "POST /login/getValidateCode HTTP/1.1" ...