【POJ 2187】Beauty Contest
【題目】
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)
【分析】
大致題意:給出一個點集,求出最遠的一對點,輸出他們距離的平方
題解:旋轉卡殼模板題
首先我們要知道,最遠的點對肯定在凸包上(應該是很顯然的吧)
想象有兩條平行線,其中有一條與原凸包的邊重合,另一條就在另一側剛剛好交上凸包的位置(可以形象地理解成這兩條線"卡"住了這個凸包),不斷地轉這兩條線(轉一圈),就能找到最遠的一對點
(這樣做應該是對的,但我不會這樣做的證明,就感性理解一下吧)
【程式碼】
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 50005
using namespace std;
int n,top;
struct point
{
int x,y;
point(){}
point(int x,int y):x(x),y(y){}
point operator+(const point &a) {return point(x+a.x,y+a.y);}
point operator-(const point &a) {return point(x-a.x,y-a.y);}
friend int dot(const point &a,const point &b) {return a.x*b.x+a.y*b.y;}
friend int cross(const point &a,const point &b) {return a.x*b.y-a.y*b.x;}
friend int dist(const point &a,const point &b) {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
}a[N],sta[N],S;
bool comp(point p,point q)
{
if(!cross(p-S,q-S))
return dot(p-S,p-S)<dot(q-S,q-S);
return cross(p-S,q-S)>0;
}
void Graham()
{
int i;
for(i=2;i<=n;++i)
if(a[1].x>a[i].x||(a[1].x==a[i].x&&a[1].y>a[i].y))
swap(a[1],a[i]);
S=a[1];sta[++top]=a[1];
sort(a+1,a+n+1,comp);
for(i=2;i<=n;++i)
{
while(top>=2&&cross(a[i]-sta[top-1],sta[top]-sta[top-1])>=0) top--;
sta[++top]=a[i];
}
}
int area(point x,point y,point z)
{
return abs(cross(y-x,z-x));
}
int next(int x)
{
if(x==top)
return 1;
return x+1;
}
int solve()
{
if(top==2)
return dist(sta[1],sta[2]);
int i,j=3,ans=0;
sta[top+1]=sta[1];
for(i=1;i<=top;++i)
{
while(next(j)!=i&&area(sta[i],sta[i+1],sta[j])<=area(sta[i],sta[i+1],sta[j+1])) j=next(j);
ans=max(ans,dist(sta[j],sta[i]));
ans=max(ans,dist(sta[j],sta[i+1]));
}
return ans;
}
int main()
{
int i,ans;
scanf("%d",&n);
for(i=1;i<=n;++i)
scanf("%d%d",&a[i].x,&a[i].y);
Graham();
ans=solve();
printf("%d",ans);
return 0;
}