Beauty Contest POJ - 2187(旋转卡壳)
Beauty Contest
POJ - 2187题意:问凸包的直径.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int inf=1e9+10; 7 const int maxn=50010; 8 9 struct Node{ 10 int x,y; 11 bool operator <(const Node &a)const{ 12 return x<a.x||(x==a.x&&y<a.y); 13 } 14 Node operator -(Node &a){ 15 return Node{x-a.x,y-a.y}; 16 } 17 }p[maxn],ch[maxn]; 18 int cross(Node a,Node b){ 19 return a.x*b.y-a.y*b.x; 20 } 21 double getlen(Node a){ 22 return a.x*a.x+a.y*a.y; 23 } 24 25 int ConvexHull(int n){ 26 sort(p,p+n); 27 int m=0; 28 for(int i=0;i<n;i++){ 29 while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; 30 ch[m++]=p[i]; 31 } 32 int k=m; 33 for(int i=n-2;i>=0;i--){ 34 while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; 35 ch[m++]=p[i]; 36 } 37 if(n>1) m--; 38 return m; 39 } 40 41 //RotatrCalipers 42 double RC(int n){ 43 double ans=-inf; 44 ch[n]=ch[0]; 45 int q=1; 46 for(int i=0;i<n;i++){ 47 while(cross(ch[i+1]-ch[i],ch[q]-ch[i])<cross(ch[i+1]-ch[i],ch[q+1]-ch[i])) q=(q+1)%n; 48 ans=max(ans,max(getlen(ch[q]-ch[i]),getlen(ch[q+1]-ch[i+1]))); 49 } 50 return ans; 51 } 52 53 int main(){ 54 int n; 55 // freopen("in.txt","r",stdin); 56 while(scanf("%d",&n)!=EOF){ 57 for(int i=0;i<n;i++) scanf("%d%d",&p[i].x,&p[i].y); 58 int m=ConvexHull(n); 59 double ans=RC(m); 60 printf("%.0f\n",ans); 61 } 62 }