poj2187--Beauty Contest(凸包首杀)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 29123 Accepted: 9046

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

USACO 2003 Fall

 

题意:给出n个点的坐标,问最远的两个点的距离的平方是什么?

首先最远的两个点一定在凸包上,求出凸包,找出里面的最远的。

求凸包:

1、首先找出最低的一个点,如果存在相同低的点,找出最左侧的点。

2、以找出的定点进行极角排序,以定点为圆心,所有的点以逆时针排,当角度相同时,按到定点的距离由小到大排。

3、极角排序后,按排序的顺序,如果叉乘大于0,压入栈中,否则弹出一个,把新点压入栈中。

4、最后栈中的点就是凸包的点。

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <algorithm>
using namespace std ;
#define eps 1e-8
struct node{
    double x , y ;
}p[51000] , q , q1 , q2 ;
stack <node> sta ;
int mul(node a,node b)
{
    double x = (a.x-q.x)*(b.y-q.y) - (a.y-q.y)*(b.x-q.x) ;
    if( fabs(x) < eps )
        return 0 ;
    else if( x > 0 )
        return 1 ;
    return -1 ;
}
int dis(node a,node b)
{
    double l1 , l2 ;
    l1 = (a.x-q.x)*(a.x-q.x) + (a.y-q.y)*(a.y-q.y) ;
    l2 = (b.x-q.x)*(b.x-q.x) + (b.y-q.y)*(b.y-q.y) ;
    if( l1 < l2 )
        return 1 ;
    else
        return 0 ;
}
node sub(node a,node b)
{
    a.x -= b.x ;
    a.y -= b.y ;
    return a ;
}
int cmp(node a,node b)
{
    int k1 = mul(a,b) , k2 = dis(a,b) ;
    return k1 == 1 || ( k1 == 0 && k2 == 1 ) ;
}
int main()
{
    int n , i , j ;
    node s , e ;
    scanf("%d", &n) ;
    q.x = q.y = 20000 ;
    for(i = 0 ; i < n ; i++)
    {
        scanf("%lf %lf", &p[i].x, &p[i].y) ;
        if( p[i].y < q.y || ( p[i].y == q.y && p[i].x < q.x ) )
            q = p[i] ;
    }
    sort(p,p+n,cmp);
    p[n] = q ;
    while( !sta.empty() ) sta.pop() ;
    sta.push(p[0]) ;
    sta.push(p[1]) ;
    for(i = 2 ; i < n ; i++)
    {
        //printf("%lf %lf**\n", p[i].x, p[i].y) ;
        q1 = sta.top() ;
        sta.pop() ;
        q2 = sta.top() ;
        sta.pop() ;
        s = sub(q1,q2) ;
        e = sub(p[i],q1) ;
        if( s.x*e.y - s.y*e.x > 0 )
        {
            sta.push(q2) ;
            sta.push(q1) ;
            sta.push(p[i]) ;
        }
        else
        {
            sta.push(q2) ;
            sta.push(p[i]) ;
        }
    }
    int num = 0 ;
    while( !sta.empty() )
    {
        p[num++] = sta.top() ;
        //printf("%lf %lf\n", p[num-1].x, p[num-1].y) ;
        sta.pop() ;
    }
    double max1 = 0 ;
    for(i = 0 ; i < num ; i++)
    {
        for(j = i+1 ; j < num ; j++)
        {
            max1 = max( max1, (p[i].x-p[j].x)*(p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y) ) ;
        }
    }
    printf("%.lf\n", max1) ;
    return 0 ;
}
/*
8
2 3
2 2
1 1
1 0
0 0
0 3
-1 2
-2 0
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值