POJ-2187-Beauty Contest

2 篇文章 0 订阅
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34373 Accepted: 10640

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)

求的是平面点集上的最远点对,实际上就是该点集的凸包的直径, 构建出凸包后枚举两点求最远距离

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct node
{
    int x;
    int y;
} point[100000];
int n;
int s[100000], top;
double chacheng(struct node a, struct node b, struct node c)
{
    return ((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x));
}
double dist(struct node a, struct node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(struct node a,struct node b)
{
    int k=chacheng(a,b,point[0]);
    if(k>0)
        return 1;
    else if(k==0&&dist(a,point[0])<dist(b,point[0]))
        return 1;
    else
        return 0;
}
void Graham()
{
    int k=0;
    int i;
    for(i=1; i<n; i++)
    {
        if (point[i].x<point[k].x||(point[i].x==point[k].x&&point[i].y<point[k].y))
        {
            k=i;
        }
    }
    swap(point[0],point[k]);
    sort(point,point+n,cmp);
    for(i=0; i<=2; i++)
    {
        s[i]=i;
    }
    top=2;
    for(i=3; i<n; i++)
    {
        while(chacheng(point[i],point[s[top]],point[s[top-1]])>=0)
        {
            top--;
            if(top==0)
            {
                break;
            }
        }
        top++;
        s[top]=i;
    }
}
int dis(node a, node b)
{
    int x = a.x-b.x, y = a.y-b.y;
    return fabs(x*x+y*y);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        Graham();
        int maxdist=0;
        for(int i=0;i<=top-1;i++)
            for(int j=i+1;j<=top;j++)
            {
                int temp=dis(point[ s[i] ],point[ s[j] ]);
                if(maxdist < temp)
                    maxdist = temp;
            }
        printf("%d\n", maxdist);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值