poj 2187 Beauty Contest(凸包+旋转卡壳)

Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 19161   Accepted: 5792

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
题目: http://poj.org/problem?id=2187
分析:这题直接求凸包,然后最长边肯定在凸包上,本来凸包求出来后,我枚举一个终点,然后另一个点不断地跟在它后面,也就是如果距离开始变短,就不断得维护后面那个点,使得距离开始变长,不知道为什么错掉。。。最后暴力枚举凸包上的点对才过掉,不过这样的话,如果数据一开始就是个凸包,估计会TLE。。。不过我终于会凸包了 T_T

代码:
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=55555;
struct point
{
    int x,y;
} g[mm],q[mm];
int i,j,ans,top,n;
int crotch(point a,point b)
{
    return a.x*b.y-b.x*a.y;
}
int muilcro(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int sqrdis(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp1(point a,point b)
{
    return (a.x<b.x)||(a.x==b.x&&a.y<b.y);
}
bool cmp(point a,point b)
{
    int k=muilcro(a,b,g[0]);
    if(k<0)return 1;
    if(k>0)return 0;
    return sqrdis(a,g[0])>=sqrdis(b,g[0]);
}
void Graham()
{
    int i,j;
    for(i=j=0; i<n; ++i)
        if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i;
    q[0]=g[0],g[0]=g[j],g[j]=q[0];
    sort(g+1,g+n+1,cmp);
    q[0]=g[0];
    q[top=1]=g[1];
    for(i=2; i<n; ++i)
    {
        while(top&&muilcro(q[top],g[i],q[top-1])>0)--top;
        q[++top]=g[i];
    }
}
int main()
{
    while(scanf("%d",&n)!=-1)
    {
        for(i=0; i<n; ++i)
            scanf("%d%d",&g[i].x,&g[i].y);
        Graham();
        for(ans=i=0;i<top;++i)
            for(j=i+1;j<=top;++j)
                ans=max(ans,sqrdis(q[i],q[j]));
        printf("%d\n",ans);
    }
    return 0;
}

现在重做了下,用上了旋转卡壳,凸包模板也改变了哈
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=55555;
typedef int diy;
struct point
{
    diy x,y;
    point(){}
    point(diy _x,diy _y):x(_x),y(_y){}
}g[mm],q[mm];
point Vector(point s,point t)
{
    return point(t.x-s.x,t.y-s.y);
}
diy CrossProduct(point P,point Q)
{
    return P.x*Q.y-P.y*Q.x;
}
diy MultiCross(point P,point Q,point R)
{
    return CrossProduct(Vector(Q,P),Vector(Q,R));
}
diy SqrDis(point P,point Q)
{
    return (P.x-Q.x)*(P.x-Q.x)+(P.y-Q.y)*(P.y-Q.y);
}
bool TurnRight(point P,point Q,point R)
{
    diy tmp=MultiCross(P,Q,R);
    if(tmp>0)return 1;
    if(tmp<0)return 0;
    return SqrDis(P,Q)<SqrDis(P,R);
}
bool cmp(point P,point Q)
{
    return TurnRight(g[0],Q,P);
}
void Graham(int n,int &m)
{
    int i,j;
    for(j=i=0;i<n;++i)
        if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i;
    swap(g[0],g[j]);
    sort(g+1,g+n,cmp);
    q[m=0]=g[n]=g[0];
    for(i=1;i<=n;++i)
    {
        while(m&&TurnRight(q[m-1],q[m],g[i]))--m;
        q[++m]=g[i];
    }
}
diy RotatingCalipers(int n)
{
    int l,r=1;
    diy ret=0;
    for(l=0;l<n;++l)
    {
        while(MultiCross(q[l+1],q[l],q[r])<MultiCross(q[l+1],q[l],q[r+1]))r=(r+1)%n;
        ret=max(ret,max(SqrDis(q[l],q[r]),SqrDis(q[l+1],q[r+1])));
    }
    return ret;
}
int main()
{
    int i,n,m;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;++i)
            scanf("%d%d",&g[i].x,&g[i].y);
        Graham(n,m);
        printf("%d\n",RotatingCalipers(m));
    }
    return 0;
}


你可能感兴趣的:(poj 2187 Beauty Contest(凸包+旋转卡壳))