poj 2187 Beauty Contest(凸包)

Description
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.

Input
* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output

2

题意:有N个牧场,求最远两个牧场的距离的平方。

如果一个一个搜索肯定会超时,因此只需枚举凸包上的点就可以了。

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,top;
struct point
{
    int x,y;
} p[50005],s[50005],t;
int cross(point t1,point t2,point t3,point t4)  //求向量t1t2和向量t3t4的叉积
{
    return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
int dis1(point t1,point t2)   //求距离平方
{
   int z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
    return z;
}
double dis(point t1,point t2)   //求距离
{
    double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
    return sqrt(z);
}
bool cmp(point t1,point t2)
{
    double z=cross(p[0],t1,p[0],t2);
    return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint()    //找基点,按y从小到大,如果y相同,按x从左到右
{
    int i,j=0;
    t=p[0];
    for(i=1; i<n; i++)
    {
        if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
        {
            j=i;
            t=p[i];
        }
    }
    t=p[0];
    p[0]=p[j];
    p[j]=t;
}
void scanner()
{
    int i;
    findpoint();
    sort(p+1,p+n,cmp);
    s[0]=p[0];
    s[1]=p[1];
    top=1;
    for(i=2; i<n; i++)
    {
        while(cross(s[top-1],s[top],s[top],p[i])<0)
            top--;
        top++;
        s[top]=p[i];
    }
}
int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {  int ans=-1;
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        scanner();
        for(i=0; i<=top; i++)
            for(j=0;j<=top;j++)
        {
            ans=max(ans,dis1(s[i],s[j]));
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值