CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)
传送门:
http://codeforces.com/problemset/problem/598/C
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 分析:
题意:
给你n个点,问你这些点和原点构成的n个向量中,哪两个向量构成的角最小
做法:
用atan2函数把所有向量与x轴正半轴的夹角求出,排序,俩俩间比较差值即可。 C 语言里long double atan2(long double y,long double x) 返回的是原点至点(x,y)的方位角,
即与 x 轴的夹角。也可以理解为复数 x+yi 的辐角。
返回值的单位为弧度,取值范围为(-PI,PI]。 需要注意的地方:
1.使用long double
2.两两比较的时候,一开始要取最大差值,就是第一个和最后一个的差值
(注意最后一个角度(最大角)和第一个角度(最小角)的角度差可能是负值,要加上2*PI)
才能比较出最小的嘛 code:
#include<bits/stdc++.h>
using namespace std;
typedef long double LB;
#define max_v 100105
#define PI acos(-1)
#define double long double // 使用long double
const int MAXN = 1e5 + ;
struct node
{
double v;
int index;
bool operator < (const node& t)const//按照v升序
{
return v < t.v;
}
} p[MAXN];
bool cmp(node a,node b)
{
return a.v<b.v;
}
int main()
{
int n;
scanf("%d",&n);
int x,y;
for(int i=; i<=n; ++i)
{ scanf("%d %d",&x,&y);
p[i].v=atan2(y,x);
p[i].index=i;
}
sort(p+,p+n+);
int ans1=p[].index;
int ans2=p[n].index;
double v=p[].v+*PI-p[n].v;
for(int i=; i<n; ++i)
{
double temp=p[i+].v-p[i].v;
if(temp<v)
{
v=temp;
ans1=p[i].index;
ans2=p[i+].index;
}
}
printf("%d %d\n",ans1,ans2);
return ;
}
CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)的更多相关文章
- CodeForces 598C Nearest vectors
这题对精度要求很高.用atan2吧... #include<iostream> #include<cstring> #include<cmath> #include ...
- codeforces 598C C. Nearest vectors(极角排序)
题目链接: C. Nearest vectors time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 1 C. Nearest vectors 极角排序
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...
- Educational Codeforces Round 1(C. Nearest vectors)
题目链接:http://codeforces.com/problemset/problem/598/C 题意是给你一个数n,下面n行,每行给你横坐标x和纵坐标y(x != 0 && y ...
- codeforces Gym 100500C C. ICPC Giveaways 排序
Problem C. ICPC GiveawaysTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- codeforces 1284E. New Year and Castle Construction(极角排序+扫描枚举)
链接:https://codeforces.com/problemset/problem/1284/E 题意:平面上有n个点,问你存在多少组四个点围成的四边形 严格包围某个点P的情况.不存在三点共线. ...
- codeforces 101C C. Vectors(数学)
题目链接: C. Vectors time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces 682B Alyona and Mex (排序+离散化)
Alyona and Mex 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/B Description Someone gave ...
随机推荐
- ztree框架使用问题汇总
1.如何让用户只能点击页子节点 var setting = { callback: { beforeClick: zTreeBeforeClick } }; function zTreeBeforeC ...
- unity向量-数学-三角函数
1.如何在unity写cos60 Mathf.Cos(Mathf.Deg2Rad * ) Deg2Rad将 60 角度转换为弧度,因为里面参数只能填弧度数 2.计算一个Vector3绕旋转中心旋转指定 ...
- Linux之FTP篇
内容简介: vsftpd的安装 目录介绍 配置参数解释 锁定用户目录 其他用户不能登录 -------------------------------------------------------- ...
- Hashtable元素的删除
2中方法 Remove(); Clear(); static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1,& ...
- PHP常用的一些数组操作总结
1.array_values() :返回包含数组中所有键值的数组,不保留键名. 2.array_diff() 函数返回两个数组的差集数组.该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的 ...
- es6变量解构赋值的用途
这里是我觉得es6解构赋值,在平时我们写js的时候非常有用,而且经常用到的地方,能简化我们的代码,让写代码简介优雅易读; 用途 1.交换变量的值,太方便了这逼,写法不仅简介而且一看就明白 let [x ...
- Bootstrap拟态框+支付宝首页
任务没完成,继续来!因为刚才网不好,我辛辛苦苦打了洋洋洒洒一大堆都没了! 我们今天主要是说一个简单的由Bootstrap和HTML5结合而成的小案例: 首先:由标题可得知,这是移动端,所以需要这样一串 ...
- Shader Example
//测试viewDir对顶点的影响Shader "Example/TestViewDir" { Properties{ _RimColor("Rim Color" ...
- ViewPager+fragment的使用
如图我在一个继承FragmentActivity的类中嵌套了3个fragment分别能实现3个不同的界面,默认展现第一个,在第一个的fragment中有个ViewPager在ViewPager中嵌套了 ...
- head标签必不可少的元素
<head> 标签用于定义文档的头部,它是所有头部元素的容器.<head> 中的元素可以引用脚本.指示浏览器在哪里找到样式表.提供元信息等等. 文档的头部描述了文档的各种属性和 ...