poj2187Beauty Contest(凸包直径)
利用旋转卡壳
参考博客http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 50010 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct point 18 { 19 int x,y; 20 point(int x=0,int y = 0):x(x),y(y){} 21 }p[N],ch[N]; 22 typedef point pointt; 23 pointt operator - (point a,point b) 24 { 25 return point(a.x-b.x,a.y-b.y); 26 } 27 int dcmp(double x) 28 { 29 if(fabs(x)<eps) return 0; 30 return x<0?-1:1; 31 } 32 double cross(point a,point b) 33 { 34 return 1.0*a.x*b.y-a.y*b.x; 35 } 36 double mul(point p0,point p1,point p2) 37 { 38 return cross(p1-p0,p2-p0); 39 } 40 int dis(point a) 41 { 42 return a.x*a.x+a.y*a.y; 43 } 44 bool cmp(point a,point b) 45 { 46 if(dcmp(mul(p[0],a,b))==0) return dis(a-p[0])<dis(b-p[0]); 47 return dcmp(mul(p[0],a,b))>0; 48 } 49 int graham(int n) 50 { 51 int i,k=0,top; 52 point tmp; 53 for(i = 0; i< n; i++) 54 { 55 if(p[i].y<p[k].y||(p[i].y==p[k].y&&p[i].x<p[k].x)) 56 k = i; 57 } 58 if(k!=0) 59 swap(p[0],p[k]); 60 sort(p+1,p+n,cmp); 61 ch[0] = p[0]; 62 ch[1] = p[1]; 63 top = 1; 64 for(i = 2; i < n; i++) 65 { 66 while(top>0&&dcmp(mul(ch[top-1],ch[top],p[i]))<=0) 67 top--; 68 ch[++top] = p[i]; 69 } 70 return top; 71 } 72 int rotating_calipers(int n) 73 { 74 int q = 1; 75 int ans = 0; 76 ch[n] = ch[0]; 77 for(int i = 0 ; i < n; i++) 78 { 79 while(mul(ch[i+1],ch[q+1],ch[i])>mul(ch[i+1],ch[q],ch[i])) 80 q = (q+1)%n; 81 ans = max(ans,max(dis(ch[i]-ch[q]),dis(ch[i+1]-ch[q+1]))); 82 } 83 return ans; 84 } 85 int main() 86 { 87 int n,i; 88 while(scanf("%d",&n)!=EOF) 89 { 90 for(i = 0; i < n;i++) 91 scanf("%d%d",&p[i].x,&p[i].y); 92 int top = graham(n); 93 int ans = rotating_calipers(top+1); 94 printf("%d\n",ans); 95 } 96 return 0; 97 }