POJ2187:Beauty Contest(凸包 & 旋转卡壳)

本文介绍了一种高效算法,用于解决在平面上找到两两之间距离最远的点对问题。通过构造凸包并利用旋转卡壳技巧,文章详细解释了如何在O(n log n)的时间复杂度内找到这一对点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 39350 Accepted: 12212

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

题意:给一堆点,找出最远的点对距离。

思路:显然该点对在凸包上,那么先求出凸包。旋转卡壳的对踵点如果过凸包上的两个点可以画一对平行直线,使凸包上的所有点都夹在两条平行线之间或落在平行线上,那么这两个点叫做一对对踵点,显然最远点对必然属于对踵点对集合,那么可以找出所有对踵点取距离最大那个,方法就是枚举每一条边a1-a2,找出ax,使得a1-a2-ax组成的三角形面积最大,这个可以通过叉积判断。又枚举的过程中面积的变化是一个凸函数,枚举下一条边时从ax开始就行了。凸包O(nlogn),旋转卡壳O(n)。

# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxn = 1e5+3;
int n;
struct node{double x, y;}a[maxn], ans[maxn];
inline node operator-(node a, node b)
{
    return node{a.x-b.x, a.y-b.y};
}
inline double operator*(node a, node b)
{
    return a.x*b.y - a.y*b.x;
}
inline double dis(node a)
{
    return a.x*a.x+a.y*a.y;
}
inline bool cmp(node x, node y)
{
    if(fabs((x-a[1])*(y-a[1]))>0) return (x-a[1])*(y-a[1])>0;
    return dis(x-a[1])<dis(y-a[1]);
}
inline double cha(node a, node b, node c)
{
    return fabs((a-b)*(a-c))/2;
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) scanf("%lf%lf",&a[i].x,&a[i].y);
    for(int i=2;i<=n;i++)
        if(a[i].y<a[1].y||(a[i].y==a[1].y&&a[i].x<a[1].x))
            swap(a[1],a[i]);
    sort(a+2,a+n+1,cmp);
    int top=2;ans[1]=a[1];ans[2]=a[2];
    for(int i=3; i<=n; ++i)
    {
        while(top>1&&(a[i]-ans[top-1])*(ans[top]-ans[top-1])>=0) --top;
        ans[++top]=a[i];
    }
    for(int i=0;i<top;i++)
        ans[i] = ans[i+1];
    n=top;
    double imax = 0;
    for(int i=0,j=2;i<n;i++)
    {
        while(cha(ans[i],ans[(i+1)%n],ans[j]) < cha(ans[i],ans[(i+1)%n],ans[(j+1)%n])) j=(j+1)%n;
        imax = max(dis(ans[i]-ans[j]),imax);
    }
    printf("%.0f",imax);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值