http://poj.org/problem?id=2187
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn=50000; 8 struct point 9 { 10 int x,y; 11 bool operator < (const point &a)const 12 { 13 return (x<a.x)||(x==a.x&&y<a.y); 14 } 15 }; 16 17 point p[maxn],ch[maxn]; 18 19 int sqr(int x) 20 { 21 return x*x; 22 } 23 24 int det(point a,point b,point c) 25 { 26 return((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)); 27 } 28 29 int dis(point a,point b) 30 { 31 return sqr(a.x-b.x)+sqr(a.y-b.y); 32 } 33 34 int convex_hull(point *p,int n,point *ch) 35 { 36 sort(p,p+n); 37 int m=0; 38 for(int i=0; i<n; i++) 39 { 40 while(m>1&&det(ch[m-2],ch[m-1],p[i])<=0) m--; 41 ch[m++]=p[i]; 42 } 43 int k=m; 44 for(int i=n-2; i>=0; i--) 45 { 46 while(m>k&&det(ch[m-2],ch[m-1],p[i])<=0) m--; 47 ch[m++]=p[i]; 48 } 49 if(n>1) m--; 50 return m; 51 } 52 53 int dia_rotating_calipers(int n) 54 { 55 int dia=0,q=1; 56 for(int i=0; i<n; i++) 57 { 58 while(det(ch[i],ch[i+1],ch[q+1])>det(ch[i],ch[i+1],ch[q])) 59 { 60 q=(q+1)%n; 61 } 62 dia=max(dia,max(dis(ch[i],ch[q]),dis(ch[i+1],ch[q+1]))); 63 } 64 return dia; 65 } 66 67 int main() 68 { 69 int n,cn; 70 scanf("%d",&n); 71 for(int i=0; i<n; i++) 72 { 73 scanf("%d%d",&p[i].x,&p[i].y); 74 } 75 cn=convex_hull(p,n,ch); 76 printf("%d ",dia_rotating_calipers(cn)); 77 return 0; 78 }