POJ2187 旋转卡壳+凸包

题意:给定一些点,求最远的点的距离。

凸包+旋转卡壳

View Code
  1 /*
  2 旋转卡壳
  3 凸包中的点为顺时针存储
  4 */
  5 #include<stdio.h>
  6 #include<string.h>
  7 #include<stdlib.h>
  8 #include<algorithm>
  9 #include<iostream>
 10 #include<queue>
 11 //#include<map>
 12 #include<math.h>
 13 using namespace std;
 14 typedef long long ll;
 15 //typedef __int64 int64;
 16 const int maxn = 50005;
 17 const int inf = 0x7fffffff;
 18 struct node{
 19     int x,y;
 20     bool operator <( const node &p ) const {
 21         return y<p.y||(y==p.y&&x<p.x);
 22     }
 23 };
 24 node pnt[ maxn ],res[ maxn ];
 25 int cross( node sp,node ep,node op ){
 26      return (sp.x - op.x) * (ep.y - op.y)-(ep.x - op.x) * (sp.y - op.y);
 27 }
 28 /*
 29 ep
 30 |
 31 |
 32 op----sp
 33 ( from sp to ep )
 34 */
 35 
 36 int dis2( node a,node b ){
 37     return (( a.x-b.x )*( a.x-b.x )+( a.y-b.y )*( a.y-b.y ));
 38 }
 39 
 40 int graham( int n ){
 41     int top=1;
 42     sort( pnt,pnt+n );//!!!!!
 43     if( n==0 ) return 0;
 44     else res[ 0 ]=pnt[ 0 ];
 45     if( n==1 ) return 1;
 46     else res[ 1 ]=pnt[ 1 ];
 47     if( n==2 ) return 2;
 48     else res[ 2 ]=pnt[ 2 ];
 49     
 50     for( int i=2;i<n;i++ ){
 51         while( top>0&&cross( res[ top ],pnt[ i ],res[ top-1 ] )>=0 )
 52             top--;
 53         res[ ++top ]=pnt[ i ];
 54     }
 55     int tmp=top;
 56     res[ ++top ]=pnt[ n-2 ];
 57     for( int i=n-3;i>=0;i-- ){
 58         while( top!=tmp&&cross( res[ top ],pnt[ i ],res[ top-1 ] )>=0 )
 59             top--;
 60         res[ ++top ]=pnt[ i ];
 61     }
 62     return top;
 63 }
 64 
 65 int rotating( int n ){
 66     int q=1;
 67     int ans=0;
 68     res[ n ]=res[ 0 ];
 69     for( int i=0;i<n;i++ ){
 70         while( cross( res[ i ],res[ q+1 ],res[ i+1 ] )>cross( res[ i ],res[ q ],res[ i+1 ] ) )    
 71             q=(q+1)%n;
 72         ans=max( ans,max( dis2( res[i],res[q] ),dis2( res[i+1],res[q+1] ) ) );
 73     }
 74     return ans;
 75 }
 76 /*
 77 void test(){
 78     int sum=0;
 79     node a,b,c;
 80     a.x=a.y=1;
 81     b.x=3,b.y=1;
 82     c.x=1,c.y=7;
 83     printf("%d\n",cross(a,b,c));
 84 }    
 85 */
 86 /*
 87 void test2( int cnt ){
 88     printf("\n");
 89     for( int i=0;i<cnt;i++ )
 90         printf("%d %d\n",res[i].x,res[i].y);
 91     printf("\n");
 92 }
 93 */
 94 int main(){
 95     int n;
 96     //test();
 97     while( scanf("%d",&n)!=EOF ){
 98         for( int i=0;i<n;i++ )
 99             scanf("%d%d",&pnt[ i ].x,&pnt[ i ].y);
100         int cnt=graham( n );
101         //test2(cnt);
102         printf("%d\n",rotating( cnt ));
103     }
104     return 0;
105 }

 

posted @ 2013-03-17 10:58  xxx0624  阅读(330)  评论(0编辑  收藏  举报