【凸包】poj 2187 Beauty Contest (旋转卡壳求平面最远点对)

来源:互联网 发布:网络推广工作内容 编辑:程序博客网 时间:2024/05/16 08:14

旋转卡壳算法原理讲解:点击打开链接

旋转卡壳算法的使用范围很广,我只学习了求平面最远点对的方法;


1.旋转卡壳求平面最远点对方法:


如果qa,qb是凸包上最远两点,必然可以分别过qa,qb画出一对平行线。通过旋转这对平行线,我们可以让它和凸包上的一条边重合,如图中蓝色直线,可以注意到,qa是凸包上离p和qb所在直线最远的点。

于是我们的思路就是枚举凸包上的所有边,对每一条边找出凸包上离该边最远的顶点,计算这个顶点到该边两个端点的距离,并记录最大的值。

直观上这是一个O(n2)的算法,和直接枚举任意两个顶点一样了。但是注意到当我们逆时针枚举边的时候,最远点的变化也是逆时针的,这样就可以不用从头计算最远点,而可以紧接着上一次的最远点继续计算。于是我们得到了O(n)的算法。


2.那么用什么条件来判定凸包上离该边最远的顶点呢,用面积:


一个对踵点和对应边之间的距离比其他点要大,也就是一个对踵点和对应边所形成的三角形是最大的;

对踵点的定义:点击打开链接

叉积的性质:两个向量的叉积的一半就是两个向量组成三角形的面积


3.代码:

int solve(){    Graham();    int q=1,ans=0;    p[tot]=p[0];    for(int i=0; i<tot; i++)    {        while(multi(p[i],p[i+1],p[q+1])>multi(p[i],p[i+1],p[q])) q=(q+1)%tot;        ans=max(ans,max(dis(p[i],p[q]),dis(p[(i+1)%tot],p[(q+1)%tot])));    }    return ans;}



Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 37982 Accepted: 11738

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

40 00 11 11 0

Sample Output

2

Hint


题意:给定平面的一系列点,求出平面最远点对间距离的平方;

思路:旋转卡壳求平面最远点对;

代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N=100005;struct node{    int x,y;} a[N],p[N];int n,tot;int dis(node a,node b){    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}int multi(node p0,node p1,node p2){    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}int cmp(node p1,node p2)//极角排序;{    int x=multi(p1,p2,a[0]);    if(x>0||(x==0&&dis(p1,a[0])<dis(p2,a[0]))) return 1;    return 0;}void Graham()//先求凸包;{    int k=0;    for(int i=0; i<n; i++)        if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x)) k=i;    swap(a[0],a[k]);    sort(a+1,a+n,cmp);    tot=2,p[0]=a[0],p[1]=a[1];    for(int i=2; i<n; i++)    {        while(tot>1&&multi(p[tot-2],p[tot-1],a[i])<=0) tot--;        p[tot++]=a[i];    }}int solve(){    Graham();    int q=1,ans=0;    p[tot]=p[0];    for(int i=0; i<tot; i++)    {        while(multi(p[i],p[i+1],p[q+1])>multi(p[i],p[i+1],p[q])) q=(q+1)%tot;  //q=(q+1)%tot;        ans=max(ans,max(dis(p[i],p[q]),dis(p[(i+1)%tot],p[(q+1)%tot])));       //取对踵点与边上两端点距离大的值;    }    return ans;}int main(){    while(~scanf("%d",&n))    {        for(int i=0; i<n; i++) scanf("%d%d",&a[i].x,&a[i].y);        int ans=solve();        printf("%d\n",ans);    }    return 0;}


/*图片转载自其他博客*/



阅读全文
0 0
原创粉丝点击