Description
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
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
Source
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;
const int maxn=50010;
struct point
{
int x,y;
}list[maxn],stack[maxn];
int dist(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int cmp(point a,point b)
{
int cur=cross(list[0],a,b);
if(cur > 0)
return 1;
else if(cur==0 && dist(a,list[0]) < dist(b,list[0]))
return 1;
else
return 0;
}
void init(int n)
{
int k=0;
point p0;
scanf("%d%d",&list[0].x ,&list[0].y);
p0.x=list[0].x;
p0.y=list[0].y;
for(int i=1;i<n;i++)
{
scanf("%d%d",&list[i].x,&list[i].y);
if(list[i].y < p0.y || (p0.y == list[i].y && list[i].x < p0.x))
{
p0.x=list[i].x;
p0.y=list[i].y;
k=i;
}
}
list[k]=list[0];
list[0]=p0;
sort(list+1,list+n,cmp);
}
int graham(int n)
{
int top;
if(n==2)
{
stack[0]=list[0];
stack[1]=list[1];
top=1;
}
else
{
stack[0]=list[0];
stack[1]=list[1];
top=1;
for(int i=2;i<n;i++)
{
while(top && cross(stack[top-1],stack[top],list[i]) <=0)
top--;
top++;
stack[top]=list[i];
}
}
return top;
}
int xuanzhuankake(int top)
{
int q=1;
int ans=0;
stack[top+1]=stack[0];
for(int p=0;p<=top;p++)
{
while(cross(stack[p],stack[p+1],stack[q]) < cross(stack[p],stack[p+1],stack[q+1]))
q=(q+1)%(top+1);//找到使得面积比当前大的点,当q+1==top+1时,q实际是0,所以要取模
ans=max(ans,max(dist(stack[p],stack[q]),dist(stack[p+1],stack[q+1])));
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init(n);
int top=graham(n);
printf("%d\n",xuanzhuankake(top));
}
return 0;
}