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个点必定能走完, ...
随机推荐
- Unity合并选中物体的Mesh
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; pu ...
- vscode 全局安装和配置 stylelint 像 webstorm 等 ide 一样来检查项目
商店里安装完插件以后全局安装或者在项目中(记得加--save-dev)安装配置: npm install stylelint-config-recommended -g 然后在vscode setti ...
- python字符串基本方法
字符串类型在Python中是十分重要的类型,他一般用引号中间添加字符的形式表达,不同于其他语言的是,Python中双引号(” “)与单引号(’ ‘)是不予区分的.都可以用来表示字符串 创建.赋值和访问 ...
- php输出缓冲区
ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush( ...
- Android笔记之使用ZXing扫描二维码
ZXing发布版下载地址:https://github.com/zxing/zxing/releases 为了能让官方Demo跑起来,先把ZXing核心部分core复制到自己的工程里 还要把andro ...
- Asp.Net 5 Web Hook
首先,然我们来看一下WebHooks是什么.WebHooks是一个协议.它们是HTTP回调技术.并且它们是"用户定义的HTTP回调".你和 (或) 您的应用程序在有什么事情发生时会 ...
- proguard 不混淆第三方jar包的问题
导入lib文件,并且用-dontwarn 和 keep class结合试试,例如: -libraryjars libs/log4j.jar -dontwarn org.apache.log4j.*-k ...
- 【转载】Spring介绍之二
Spring框架,是进行对象管理,对象关联,解耦的一个中间层框架.SSH(Struts+Spring+Hibernate)三大Spring在中间就起着一个承上启下的作用.好,首先我们先来看一下Spri ...
- JS一些碎知识点
一些js基本知识点 Doctype 浏览器渲染模式 渲染模式发展历史 在多年以前(IE6诞生以前),各浏览器都处于各自比较封闭的发展中(基本没有兼容性可谈).随着WEB的发展,兼容性问题的解决越来越显 ...
- 【leetcode刷题笔记】3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...