Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
#include<cstdio>
#include<cmath>
#include<algorithm>
#define MAX 50010
using namespace std;
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
};
Point P[MAX],ch[MAX];
typedef Point Vector;
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
bool operator <(const Point &a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double Length(Vector A)
{
return A.x*A.x+A.y*A.y;
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
int ConvexHull(Point *p,int n)
{
sort(p,p+n);
int m=;
for(int i=;i<n;i++)
{
while(m>&&Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--)
{
while(m>k&&Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
if(n>) m--;
return m;
}
double rotating_calipers(int n)
{
int q=;
double ans=0.0;
ch[n]=ch[];
for(int p=;p<n;p++) //p是边 q是点
{
while(Cross(ch[p+]-ch[p],ch[q+]-ch[p])>Cross(ch[p+]-ch[p],ch[q]-ch[p]))
q=(q+)%n;
ans=max(ans,max(Length(ch[p]-ch[q]),Length(ch[p+]-ch[q])));
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%lf%lf",&P[i].x,&P[i].y);
int m=ConvexHull(P,n);
double ans=rotating_calipers(m);
printf("%0.0lf\n",ans);
}
return ;
}
//注意不能像C题uva10652一样用pc,因为那个题里是pc++,这里pc是0。