# 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

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,top;
struct point
{
int x,y;
} p[50005],s[50005],t;
int cross(point t1,point t2,point t3,point t4)  //求向量t1t2和向量t3t4的叉积
{
return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x);
}
int dis1(point t1,point t2)   //求距离平方
{
int z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return z;
}
double dis(point t1,point t2)   //求距离
{
double z=(t2.x-t1.x)*(t2.x-t1.x)+(t2.y-t1.y)*(t2.y-t1.y);
return sqrt(z);
}
bool cmp(point t1,point t2)
{
double z=cross(p[0],t1,p[0],t2);
return z?z>0:dis(p[0],t1)>dis(p[0],t2);
}
void findpoint()    //找基点，按y从小到大，如果y相同，按x从左到右
{
int i,j=0;
t=p[0];
for(i=1; i<n; i++)
{
if(t.y>p[i].y||(t.y==p[i].y&&t.x>p[i].x))
{
j=i;
t=p[i];
}
}
t=p[0];
p[0]=p[j];
p[j]=t;
}
void scanner()
{
int i;
findpoint();
sort(p+1,p+n,cmp);
s[0]=p[0];
s[1]=p[1];
top=1;
for(i=2; i<n; i++)
{
while(cross(s[top-1],s[top],s[top],p[i])<0)
top--;
top++;
s[top]=p[i];
}
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{  int ans=-1;
for(i=0; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
scanner();
for(i=0; i<=top; i++)
for(j=0;j<=top;j++)
{
ans=max(ans,dis1(s[i],s[j]));
}
printf("%d\n",ans);
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：poj 2187 Beauty Contest(凸包) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)