【CodeForces 618C】Constellation
2016-02-11 23:08
351 查看
题
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1to n. For each i, the i-th star is located at coordinates (xi, yi). No two stars are located at the same position.
In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions.
It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.
Input
The first line of the input contains a single integer n (3 ≤ n ≤ 100 000).
Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109).
It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.
Output
Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem.
If there are multiple possible answers, you may print any of them.
Sample test(s)
input
output
input
output
Note
In the first sample, we can print the three indices in any order.
In the second sample, we have the following picture.
![](http://codeforces.com/predownloaded/7a/6d/7a6d9ac8cd1b078f9ae1a572198f682682dd17fd.png)
Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border).
解释来了,原来是我共线的判断方法写错了,改了一下也可以AC。详情看补充的代码↓。
正确的做法是把所有点按照xy坐标排序,先排x,x相同考虑y。
然后选取第一个点第二个点,然后第三个点看看是否共线,是则看第四个点...
判断是否共线就是斜率相同,(y1-y2)/(x1-x2)==(y3-y2)/(x3-x2)那就是(y1-y2)*(x3-x2)==(x1-x2)*(y3-y2)
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1to n. For each i, the i-th star is located at coordinates (xi, yi). No two stars are located at the same position.
In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions.
It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.
Input
The first line of the input contains a single integer n (3 ≤ n ≤ 100 000).
Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109).
It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.
Output
Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem.
If there are multiple possible answers, you may print any of them.
Sample test(s)
input
3 0 1 1 0 1 1
output
1 2 3
input
5 0 0 0 2 2 0 2 2 1 1
output
1 3 5
Note
In the first sample, we can print the three indices in any order.
In the second sample, we have the following picture.
![](http://codeforces.com/predownloaded/7a/6d/7a6d9ac8cd1b078f9ae1a572198f682682dd17fd.png)
Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border).
题意
给出n个点的坐标,找出任意三个点符合组成的三角形内不包含别的点分析
我最先想的是选定第一个点,然后求别的点到它的距离,然后选离最近的点和第二近的点,共线的话去看第三近....但是这样WA了,为什么呢,待解释。。解释来了,原来是我共线的判断方法写错了,改了一下也可以AC。详情看补充的代码↓。
正确的做法是把所有点按照xy坐标排序,先排x,x相同考虑y。
然后选取第一个点第二个点,然后第三个点看看是否共线,是则看第四个点...
判断是否共线就是斜率相同,(y1-y2)/(x1-x2)==(y3-y2)/(x3-x2)那就是(y1-y2)*(x3-x2)==(x1-x2)*(y3-y2)
代码
#include <stdio.h> #include <algorithm> #define F(a,b,c) for(int a=b;a<=c;a++) #define N 100005 #define ll long long using namespace std; ll n,m=3; struct num{ll x,y,id;}a ; int cmp(num a,num b){return a.x<b.x||a.x==b.x&&a.y<b.y;} int main() { scanf("%lld",&n); F(i,1,n) scanf("%lld%lld",&a[i].x,&a[i].y),a[i].id=i; sort(a+1,a+n+1,cmp); while((a[1].x-a[2].x)*(a[1].y-a[m].y)==(a[1].y-a[2].y)*(a[1].x-a[m].x))m++; printf("%lld %lld %lld",a[1].id,a[2].id,a[m].id); return 0; }
补充的代码
#include <stdio.h> #include <algorithm> #define F(a,b,c) for(int a=b;a<=c;a++) #define N 100005 #define ll long long using namespace std; ll n,x ,y ,a=3; struct num { double v; ll x,y,id; } d ; int cmp(num a,num b) { return a.v<b.v; } ll jl(ll a,ll b) { return (d[a].x-d[b].x)*(d[a].x-d[b].x)+(d[a].y-d[b].y)*(d[a].y-d[b].y); } int main() { scanf("%I64d",&n); F(i,1,n) { scanf("%I64d%I64d",&d[i].x,&d[i].y); if(i>1) d[i].v=jl(i,1),d[i].id=i; } sort(d+2,d+n+1,cmp); while((d[1].y-d[a].y)*(d[1].x-d[2].x)==(d[1].x-d[a].x)*(d[1].y-d[2].y)&&a<n)a++; printf("1 %I64d %I64d",d[2].id,d[a].id); return 0; }
相关文章推荐
- java基础12多线程
- 9. Palindrome Number LeetCode
- bzoj1005: [HNOI2008]明明的烦恼
- 基于SDP的提议/应答(offer/answer)模型简介
- javascript笔记7-事件
- 【Codeforces 282E】Sausage Maximization 中文题意&题解&代码(C++)
- Linux文件和目录的读、写、执行权限总结
- Atitit..net clr il指令集 以及指令分类 与指令详细说明
- 112. Path Sum LeetCode
- 9.数据结构之二叉树
- Atitit..net clr il指令集 以及指令分类 与指令详细说明
- Atitit..net clr il指令集 以及指令分类 与指令详细说明
- HDU 2126(01背包扩展,记录方案数)
- java中的构造方法
- POJ 3237 Tree 树链剖分
- 第22讲-控制结构与流程图-练习
- Atitit.变量的定义 获取 储存 物理结构 基本类型简化 隐式转换 类型推导 与底层原理 attilaxDSL
- Atitit.变量的定义 获取 储存 物理结构 基本类型简化 隐式转换 类型推导 与底层原理 attilaxDSL
- Atitit.变量的定义 获取 储存 物理结构 基本类型简化 隐式转换 类型推导 与底层原理 attilaxDSL
- Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js