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个点必定能走完, ...
随机推荐
- parse arguments in bash
There are lots of ways to parse arguments in sh. Getopt is good. Here's a simple script that parses ...
- Shiro 认证失败返回JSON
Shiro框架默认认证失败后会返回到登录页面,在前后端分离项目中,需要返回JSON数据,以便前端或者app端解析处理. 实现方式: 1. 扩展shiro框架的UserFilter类,重写redirec ...
- Android中打包JAR时获取资源ID的方法
前言:在打包android源码的时,有的时候源码中包含了资源文件,但是jar包中不包含,所以会异常,解决的方案就是不用系统的提供的id名,而是直接 获取id,如反射. 1.系统提供的方法: /** * ...
- nginx(Window下安装 & 配置文件参数说明 & 实例)
一.为什么需要对Tomcat服务器做负载均衡: Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果有超过500的并发数便会出现Tomcat不能响应新的请求的情况,严重影响网站 ...
- Lumen开发:结合Redis实现消息队列(1)
1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...
- 生成n个元素的全排列 C实现
近期在准备复习算法设计的考试,下边记录一些,看笔记时突然想到的解法. 问题是这种 用递归实现 n 个元素的全排列. 当时老师给出的解答是 假定第i个元素 ri 放在首位,于是 f(r1,r2,-,rn ...
- DrawRightEditText自定义EditText实现有内容时右侧图标按钮显示无内容时右侧图标按钮隐藏加上为空时晃动动画(二)
经过大神指导,上面封装的还不够全面,触摸事件应该也放进自定义中去,那么问题来了,怎么区分呢!,这就涉及到了自定义属性的介绍了 我通过设置属性来判断在onTouch事件中应该进行什么操作,接下来看看改良 ...
- 像使用linux一样使用mac
1 不能像使用windows一样使用mac 因为mac卸载不方便. 2 gcc的问题 就使用系统默认的gcc,即clang,要想使用原声的gcc是不行的,mac本身不支持.
- Android笔记之AsyncTask
AsyncTask中的4个回调 onPreExecute(),在doInBackground(Params...)之前运行在UI线程中 onPostExecute(Result),在doInBackg ...
- 读取ByteBuffer有效的数据
转:https://zhidao.baidu.com/question/427134449349230532.html说道 ByteBuffer的缓冲区,就需要知道缓冲区的的三个状态1)capacit ...