CodeForces 618 C. Constellation(计算几何)

来源:互联网 发布:手机点击软件快速 编辑:程序博客网 时间:2024/05/17 13:10

Description
给出n个整点的坐标,要求找到三个点使得这三个点可以组成一个三角形且其余所有点都严格在这个三角形外,保证解存在
Input
第一行为一整数n表整点个数,之后n行每行两个整数xi和yi表示该整点坐标(3<=n<=100000,-10^9<=xi,yi<=10^9)
Output
输出满足条件的三个点的编号
Sample Input
5
0 0
0 2
2 0
2 2
1 1
Sample Output
1 3 5
Solution
简单计算几何,给点排序,固定排序之后的前两个点去枚举第三个点检查是否能够构成三角形即可
Code

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;#define maxn 111111typedef long long ll;struct node{    ll x,y;    int id;}p[maxn];int n;int cmp(node a,node b){    if(a.x!=b.x)return a.x<b.x;    return a.y<b.y;}bool check(node a,node b,node c){    return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x)!=0;}int main(){    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)scanf("%I64d%I64d",&p[i].x,&p[i].y),p[i].id=i;        sort(p+1,p+n+1,cmp);        for(int i=3;i<=n;i++)            if(check(p[1],p[2],p[i]))            {                printf("%d %d %d\n",p[1].id,p[2].id,p[i].id);                break;            }    }    return 0;}
0 0
原创粉丝点击