Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 41266 | Accepted: 12779 |
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.
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
* 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<algorithm>
using namespace std;
const int maxn=50005;
int top;
struct node
{
int x,y;
}c[maxn],st[maxn];
int n;
int cal(node s,node a,node b)
{
return (b.y-s.y)*(a.x-s.x)-(a.y-s.y)*(b.x-s.x);
}
int dist(node a,node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int get_ss(int i,int j,int k)//计算三点围成三角形的面积
{
node a,b;
a.x=st[j].x-st[i].x;
a.y=st[j].y-st[i].y;
b.x=st[k].x-st[i].x;
b.y=st[k].y-st[i].y;
return abs(a.x*b.y-a.y*b.x);
}
bool cmp(const node &a,const node &b)
{
int m=cal(c[0],a,b);
if(m>0) return 1;
if(m==0&&dist(c[0],b)>dist(c[0],a)) return 1;
return 0;
}
void graham()
{
st[0]=c[0];
st[1]=c[1];
top=1;
for(int i=2;i<n;i++)
{
while(top>0&&cal(st[top-1],st[top],c[i])<=0)
top--;
st[++top]=c[i];
}
st[++top]=c[0];
}
int kake()
{
int d=0,p=1;
for(int i=0;i<top;i++)
{
while(get_ss(i,i+1,p+1)>get_ss(i,i+1,p))
p=(p+1)%top;
d=max(d,max(dist(st[i],st[p]),dist(st[i+1],st[p])));
}
return d;
}
int main()
{
scanf("%d",&n);
int id=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&c[i].x,&c[i].y);
if(c[i].y<c[id].y)id=i;
else if(c[i].y==c[id].y&&c[i].x<c[id].x) id=i;
}
swap(c[0],c[id]);
sort(c+1,c+n,cmp);//极角排序
graham();
printf("%d\n",kake());
return 0;
}