POJ 2187 Beauty Contest(凸包求最远点距离)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 30924 Accepted: 9572

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
给你一些点,求最远的两点距离的平方值
#include <iostream>     //G++
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#define inf 0x3f3f3f3f
#define N 50010
using namespace std;
struct Point
{
    int x, y;
    int dis;
} pt[N], stack[N],p0;
int top, tot;
int Dis(int x1 ,int y1, int x2, int y2)
{
    return  (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)  ;
}
int Cmp_PolarAngel(struct Point p1, struct Point p2, struct Point pb)
{
    int delta = (p1.x - pb.x) * (p2.y - pb.y) - (p2.x - pb.x) * (p1.y - pb.y);
    if(delta < 0) return 1;
    if(delta == 0) return 0;
    return -1;
}
bool Is_LeftTurn(struct Point p3, struct Point p2, struct Point p1)
{
    int type = Cmp_PolarAngel(p3, p1, p2);
    if(type < 0) return true;
    return false;
}
int cmp(const void *p1, const void *p2)
{
    struct Point *a1 = (struct Point*) p1;
    struct Point *a2 = (struct Point*) p2;
    int type = Cmp_PolarAngel(*a1, *a2, p0);
    if(type < 0) return -1;
    if(type == 0)
    {
        if(a1->dis < a2->dis) return -1;
        if(a1->dis == a2->dis) return 0;
        return 1;
    }
    return 1;
}
void Hull(int n)
{
    int k;
    p0.x = inf;
    p0.y = inf;
    for(int i = 0; i < n; i++)
    {
        scanf("%d%d", &pt[i].x, &pt[i].y);
        if(pt[i].y < p0.y)
        {
            p0.y = pt[i].y;
            p0.x = pt[i].x;
            k = i;
        }
        else if(pt[i].y == p0.y)
        {
            if(pt[i].x < p0.x)
            {
                p0.x = pt[i].x;
                k = i;
            }
        }
    }
    pt[k] = pt[0];
    pt[0] = p0;
    for(int i = 1; i < n; i++)
    {
        pt[i].dis = Dis(pt[i].x, pt[i].y, p0.x, p0.y);
    }
    qsort(pt+1, n-1, sizeof(struct Point), cmp);
    tot = 1;
    for(int i = 2; i < n; i++)
    {
        if(Cmp_PolarAngel(pt[i], pt[i-1], p0))
            pt[tot++] = pt[i-1];
    }
    pt[tot++] = pt[n-1];
    top = 1;
    stack[0] = pt[0];
    stack[1] = pt[1];
    for(int i = 2; i < tot; i++)
    {
        while(top >= 1 && Is_LeftTurn(pt[i], stack[top], stack[top - 1]) == false )
        {
            top--;
        }
        stack[++top] = pt[i];
    }
}
int GrossProduct(struct Point p1, struct Point p2, struct Point p3)
{
    return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) *(p1.y - p3.y);
}
void Rotate(struct Point *ch, int n)
{
    int p = 1;
    int t1, t2, dif;
    int ans = 0;
    ch[n] = ch[0];
    for( int i = 0; i < n; i++)
    {
        while( fabs(GrossProduct(ch[i], ch[i+1], ch[p+1])) > fabs(GrossProduct(ch[i], ch[i+1], ch[p])))
        {
            p = (p + 1) % n;
        }
        dif = fabs(GrossProduct(ch[i], ch[i+1], ch[p+1])) > fabs(GrossProduct(ch[i], ch[i+1], ch[p]));
        if(dif == 0)
        {
            t1 = Dis(ch[p].x, ch[p].y, ch[i].x, ch[i].y);
            t2 = Dis(ch[p + 1].x, ch[p + 1].y, ch[i + 1].x, ch[i + 1].y);
            ans = max(ans, t1);
            ans = max(ans, t2);
        }
        else if(dif < 0)
        {
            t1 = Dis(ch[p].x, ch[p].y, ch[i].x, ch[i].y);
            ans = max(ans, t1);
        }
    }
    printf("%d\n", ans);

}
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        Hull(n);
        Rotate(stack , top + 1);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值