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个点必定能走完, ...
随机推荐
- 全文检索引擎Solr的配置
描述: 在Linux环境下实现高速的全文检索 一.当前环境: CentOS (Linux) 6.3 64 bit 二.所需软件 1.Java的JDK Java jdk 1.7.0[注意:solr5.x ...
- Android屏幕密度(Density)和分辨率概念详解
移动设备有大有小,那么如何适应不同屏幕呢,这给我们编程人员造成了很多困惑.我也是突然想到这些问题,然后去网上搜搜相关东西,整理如下. 首先,对下面这些长度单位必须了解. Android中的长度单位 ...
- C语言中的编译时分配内存
1.栈区(stack) --编译器自动分配释放,主要存放函数的参数值,局部变量值等: 2.堆区(heap) --由程序员分配释放: 3.全局区或静态区 --存放全局变量和静态变量:程序结束时由系统释放 ...
- http => https 升级
准备证书 阿里云安全(云盾)-> CA证书服务,购买证书,个人测试的话可以使用免费的,期限1年. 购买证书后,把域名与证书进行绑定,提交审核,大概10分钟左右,正常情况下审核就可以通过.证书准备 ...
- data standardization
import random import numpy as np l, num, gen_min_, gen_max_ = [], 100, 1, 200 l = [random.randint(ge ...
- centos下安装pip时失败:
[root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, securityLoading m ...
- 基于PI的Webservice发布实例
[转自http://blog.csdn.net/yin_chuan_lang/article/details/6706816] 最近的项目中,接口较多,而Webservice技术是主要实现方式之一.下 ...
- windows10 Python2和Python3共存
通过配置环境变量,达到使用python命令启动python2,使用python3命令启动python3,pip启动pip2, pip3启动pip3的目的,互不影响. 1.安装python2.7 安装 ...
- python基础16 ----面向对象程序设计二
一.继承与派生 1.继承的定义:继承是一种创建新类的方式,即在类中提取共同的部分创建出一个类,这样的类称为父类,也可称为基类和超类,新建的类称为派生类或子类. 2.单继承:就相当于子类继承了一个父类. ...
- pycharm ctrl+滚轮调节字体大小
File --> Setting --> Editor --> General --> 勾选Change font size (zoom) with Ctrl+Mouse Wh ...