[poj2187]Beauty Contest--计算几何,平面最远点对,旋转卡壳

本文介绍了一种计算平面上N个农场间最远距离的算法,通过构造凸包并利用旋转卡壳技巧,有效地找到了最远点对的距离。

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

题目描述

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>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 50010
using namespace std;
struct P{
    double x,y; 
}p[maxn];
P operator - (P a,P b)
{
    P tmp;
    tmp.x=a.x-b.x;tmp.y=a.y-b.y;
    return tmp;
}
double operator * (P a,P b)
{
    return a.x*b.y-b.x*a.y;
}
double dist(P a,P b)
{
    P tmp=a-b;
    return sqrt(tmp.x*tmp.x+tmp.y*tmp.y);
}
bool cmp(P p1,P p2)
{
    double s=(p1-p[1])*(p2-p[1]);
    return s>0||(s==0&&dist(p1,p[1])>=dist(p2,p[1]));
}
int n;
int q[maxn],top;
bool judge(int p0,int p1,int p2)
{
    double s=(p[p1]-p[p0])*(p[p2]-p[p0]);
    return s>0||(s==0&&dist(p[p1],p[p0])>=dist(p[p2],p[p0]));
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)  scanf("%lf%lf",&p[i].x,&p[i].y);
    int fir=1;
    for(int i=2;i<=n;i++)
    {
        if(p[i].y<p[fir].y||(p[i].y==p[fir].y&&p[i].x<p[fir].x)){
            fir=i;
        }
    }
    swap(p[fir],p[1]);
    sort(p+2,p+n+1,cmp);
    p[n+1]=p[1];
    q[++top]=1;q[++top]=2;
    for(int i=3;i<=n+1;i++)
    {
        while(top>1&&judge(q[top-1],i,q[top])==true)  top--;
        q[++top]=i; 
    }//以上为求凸包过程
    int now=2;//now为答案指针
    double ans=0.0;
    for(int i=1;i<top;i++)
    {
        while((p[q[i+1]]-p[q[i]])*(p[q[now]]-p[q[i]])<(p[q[i+1]]-p[q[i]])*(p[q[now+1]]-p[q[i]]))//判断当前答案和下一个点答案大小
        {
            now++;
            if(now==top)  now=1;
        }
        ans=max(ans,dist(p[q[now]],p[q[i]]));//更新答案
    }
    printf("%d\n",(int)(ans*ans)); 
    return 0;
}

谢谢大家!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值