POJ 2187Beauty Contest(凸包)

Beauty Contest

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36032 Accepted: 11174

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

题意:凸包上距离最远的两个点的距离。

题解:求凸包,再求距离。

如何求凸包那呢?
(百度百科)

用的是Graham's Scan法,这个算法是由数学大师葛立恒(Graham)发明的

过程
⒈ 在所有点中选取y坐标最小的一点H,当作基点。如果存在多个点的y坐标都为最小值,则选取x坐标最小的一点。坐标相同的点应排除。然后按照其它各点p和基点构成的向量<H,p>;与x轴的夹角进行排序,夹角由大至小进行顺时针扫描,反之则进行逆时针扫描。实现中无需求得夹角,只需根据余弦定理求出 向量夹角的余弦值即可。以下图为例,基点为H,根据 夹角由小至大排序后依次为H,K,C,D,L,F,G,E,I,B,A,J。下面进行逆时针扫描。
⒉ 线段<H,K>;一定在凸包上,接着加入C。假设线段<K,C>;也在凸包上,因为就H,K,C三点而言,它们的凸包就是由此三点所组成。但是接下来加入D时会发现,线段<K,D>;才会在凸包上,所以将线段<K,C>;排除,C点不可能是凸包。
⒊ 即当加入一点时,必须考虑到前面的线段是否会出现在凸包上。从基点开始,凸包上每条相临的 线段的旋转方向应该一致,并与扫描的方向相反。如果发现新加的点使得新线段与上线段的旋转方向发生变化,则可判定上一点必然不在凸包上。实现时可用向量 叉积进行判断,设新加入的点为pn + 1,上一点为pn,再上一点为pn - 1。顺时针扫描时,如果向量<pn - 1,pn>;与<pn,pn + 1>;的叉积为正(逆时针扫描判断是否为负),则将上一点删除。删除过程需要回溯,将之前所有叉积符号相反的点都删除,然后将新点加入凸包。
在上图中,加入K点时,由于线段<H,C>要旋转到<H,K>的角度,为顺时针旋转,所以C点不在凸包上,应该删除,保留K点。接着加入D点,由于线段<K,D>要旋转到<H,K>的角度,为逆时针旋转,故D点保留。按照上述步骤进行扫描,直到点集中所有的点都遍历完成,即得到凸包。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

int n, top;
struct node
{
    int x, y;
}a[51000], stact[51000], pointa;

int cross(struct node a, struct node b, struct node c)
{
   return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x);
}
int cmp1(struct node a, struct  node b)
{
    if(a.y == b.y)
        return a.x<b.x;
    return a.y<b.y;
}

int cmp2(struct node a, struct  node b)
{
    int t = cross(pointa, a, b);
    if(t > 0)
        return 1;
    if(t < 0)
        return 0;
    return pow(a.x - pointa.x, 2) - pow(a.y - pointa.y, 2) > pow(b.x - pointa.x, 2) - pow(b.y - pointa.y, 2);
}

void init()
{
    sort(a, a+n, cmp1);
    pointa = a[0];
    a[n] = a[0];
    sort(a+1, a+n, cmp2);
    int i;
    stact[0] = a[0];
    stact[1] = a[1];
    stact[2] = a[2];
    top = 2;
    for(i = 3; i <= n; i++)
    {
        while(top>=2 && cross(stact[top-1], stact[top], a[i]) <= 0)
        {
            top--;
        }
        stact[++top] = a[i];
    }
}

int main()
{
    int i, j;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
        {
            scanf("%d %d", &a[i].x, &a[i].y);
        }
        init();
        int Max = 0;
        for(i = 0; i <= top; i++)
        {
            for(j = 0; j <= top; j++)
            {
                if(Max < pow(stact[i].x - stact[j].x, 2)+pow(stact[i].y - stact[j].y, 2))
                {
                    Max = pow(stact[i].x - stact[j].x, 2)+pow(stact[i].y - stact[j].y, 2);
                }
            }
        }
        cout<<Max<<endl;
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值