天天看點

poj 2187 Beauty Contest

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34928 Accepted: 10818

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

提示

解析源于書。。。

題意:

一個農場有N個房子(2<=N<=50000),問最遠的房子相距多少距離。

思路:

這就是旋轉卡殼的一個最基礎的應用,通過尋找所有的對踵點,找出最遠點對(最遠點對距離的平方)。

graham求凸包和旋轉卡殼都可以做,graham時間複雜度也不高。

示例程式

graham求凸包:

Source Code

Problem: 2187		Code Length: 1447B
Memory: 784K		Time: 94MS
Language: G++		Result: Accepted
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct point
{
    int x,y;
}p[50000],tubao[50000],t;					//tubao[]記錄凸包的定點
int dis(struct point a,struct point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(struct point a,struct point b,struct point o)
{
    return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
int cmp(struct point a,struct point b)
{
    if(cross(a,b,p[0])>0)
    {
        return 1;
    }
    else if(cross(a,b,p[0])==0&&dis(a,p[0])<dis(b,p[0]))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int gra(int n)
{
    int i,pos=0,top=2;
    for(i=1;n>i;i++)
    {
        if(p[i].y<p[pos].y||(p[i].y==p[pos].y&&p[i].x<p[pos].x))
        {
            pos=i;
        }
    }
    t=p[0];
    p[0]=p[pos];
    p[pos]=t;
    sort(p+1,p+n,cmp);					//以極坐标排序
    tubao[0]=p[0];
    tubao[1]=p[1];
    tubao[2]=p[2];
    for(i=3;n>i;i++)
    {
        while(top>1&&cross(p[i],tubao[top],tubao[top-1])>=0)
        {
            top--;
        }
        top++;
        tubao[top]=p[i];
    }
    return top;
}
int main()
{
    int n,i,i1,top,maxd=-1,d;
    scanf("%d",&n);
    for(i=0;n>i;i++)
    {
        scanf("%d %d",&p[i].x,&p[i].y);
    }
    top=gra(n);					//求凸包
    for(i=0;top>=i;i++)
    {
        for(i1=i+1;top>=i1;i1++)
        {
            d=dis(tubao[i],tubao[i1]);				//計算距離
            if(maxd<d)
            {
                maxd=d;
            }
        }
    }
    printf("%d",maxd);
    return 0;
}
           

旋轉卡殼(O(n)更慢了,是書上代碼不對還是有其他原因):

Source Code

Problem: 2187		Code Length: 2048B
Memory: 784K		Time: 110MS(94MS)			//括号裡為O(n^2)算法所用時間
Language: G++		Result: Accepted
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct point
{
    int x,y;
}p[50000],ch[50000];
int det(int x1,int y1,int x2,int y2)
{
    return x1*y2-x2*y1;
}
int side(struct point a,struct point b,struct point p)
{
    return det(b.x-a.x,b.y-a.y,p.x-a.x,p.y-a.y);
}
int square_dis(struct point a,struct point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(struct point a,struct point b)
{
    if(a.y<b.y)
    {
        return 1;
    }
    else if(a.y==b.y&&a.x<b.x)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int convex_hull(int n)
{
    int i,ix,t;
    sort(p,p+n,cmp);
    ch[0]=p[0];
    if(n==1)
    {
        ch[1]=ch[0];
        return 1;
    }
    ch[1]=p[1];
    if(n==2)
    {
        ch[2]=ch[0];
        return 2;
    }
    ix=2;
    for(i=2;n>i;i++)
    {
        while(ix>1&&side(ch[ix-2],ch[ix-1],p[i])<=0)                   //<=0 for no more than 3 points in a line
        {
            ix--;
        }
        ch[ix]=p[i];
        ix++;
    }
    t=ix;
    ch[ix]=p[n-2];
    ix++;
    for(i=n-3;i>=0;i--)
    {
        while(ix>t&&side(ch[ix-2],ch[ix-1],p[i])<=0)
        {
            ix--;
        }
        ch[ix]=p[i];
        ix++;
    }
    return ix-1;
}
//O(n^2)
/*int dia_rotating_calipers(int n)
{
    int i,j,t,dia=0;
    for(i=0;n>i;i++)
    {
        for(j=0;n>j;j++)
        {
            t=square_dis(ch[i],ch[j]);
            if(t>dia)
            {
                dia=t;
            }
        }
    }
    return dia;
}*/
//O(n)
int dia_rotating_calipers(int n)
{
    int i,q=1,dia=0;
    for(i=0;n>i;i++)
    {
        while(side(ch[i],ch[i+1],ch[q+1])>side(ch[i],ch[i+1],ch[q]))
        {
            q=(q+1)%n;
        }
        dia=max(dia,max(square_dis(ch[i],ch[q]),square_dis(ch[i+1],ch[q])));
    }
    return dia;
}
int main()
{
    int i,n,cn;
    scanf("%d",&n);
    for(i=0;n>i;i++)
    {
        scanf("%d %d",&p[i].x,&p[i].y);
    }
    cn=convex_hull(n);
    printf("%d",dia_rotating_calipers(cn));
    return 0;
}