POJ 2187 Beauty Contest

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 26743 Accepted: 8258

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

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

Source


旋转卡壳求凸包直径
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
#define N 51000
using namespace std;
struct num
{
    int x,y;
}a[N],res[N];
int cross(int x1,int y1,int x2,int y2)
{
    return x1*y2-y1*x2;
}
int dis(int x1,int y1)
{
    return x1*x1+y1*y1;
}
int cmp(const void *pt1,const void *pt2)
{
    struct num *p1 =(struct num *)pt1;
    struct num *p2 =(struct num *)pt2;
    int k = cross(p1->x-a[0].x,p1->y-a[0].y,p2->x-a[0].x,p2->y-a[0].y);
    if(k>0)
    {
        return -1;
    }else if(k<0)
    {
        return 1;
    }else
    {
        return dis(p1->x-a[0].x,p1->y-a[0].y)-dis(p2->x-a[0].x,p2->y-a[0].y);
    }
}
int main()
{
    //freopen("data.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int Minx =  10001;
        int Miny = 10001;
        int pos;
        for(int i=0;i<=n-1;i++)
        {
            scanf("%d %d",&a[i].x,&a[i].y);
            if(a[i].y<Miny)
            {
                Miny = a[i].y;
                Minx = a[i].x;
                pos = i;
            }else if(a[i].y==Miny)
            {
                if(a[i].x<Minx)
                {
                    Minx = a[i].x;
                    pos = i;
                }
            }
        }
        if(n==0||n==1)
        {
            printf("0\n");
            continue;
        }
        swap(a[0].x,a[pos].x);
        swap(a[0].y,a[pos].y);
        qsort(a+1,n-1,sizeof(a[0]),cmp);
        int Top = 0;
        for(int i=0;i<=n-1;i++)
        {
            while(true)
            {
                if(Top<2)
                {
                    res[Top].x = a[i].x;
                    res[Top++].y = a[i].y;
                    break;
                }
                int x1 = res[Top-1].x-res[Top-2].x;
                int y1 = res[Top-1].y-res[Top-2].y;
                int x2 = a[i].x - res[Top-1].x;
                int y2 = a[i].y - res[Top-1].y;
                int k = cross(x1,y1,x2,y2);
                if(k>0)
                {
                    res[Top].x = a[i].x;
                    res[Top++].y = a[i].y;
                    break;
                }else
                {
                    Top--;
                }
            }
        }
        int p = 1;
        int Max = 0;
        for(int i=0;i<=Top-1;i++)
        {
            int x1 = res[(i+1)%Top].x-res[i].x;
            int y1 = res[(i+1)%Top].y-res[i].y;
            while(abs(cross(x1,y1,res[p].x-res[i].x,res[p].y-res[i].y))<abs(cross(x1,y1,res[(p+1)%Top].x-res[i].x,res[(p+1)%Top].y-res[i].y)))
            {
                p = (p+1)%Top;
            }
            Max = max(Max,dis(res[i].x-res[p].x,res[i].y-res[p].y));
            Max = max(Max,dis(res[(i+1)%Top].x-res[p].x,res[(i+1)%Top].y-res[p].y));
        }
        printf("%d\n",Max);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值