链接:http://poj.org/problem?id=2187
Description
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.
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.
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
* Line 1: A single integer, N
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
4 0 0 0 1 1 1 1 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
============================================
没有用旋转卡壳的算法,直接暴力求的最远点对,枚举每一对点
不知是数据太弱还是暴力可以过,感觉这样可能超时,但只用了313ms
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <math.h> 7 8 using namespace std; 9 10 const int MAX=50010; 11 const double eps=1e-6; 12 typedef struct point 13 { 14 double x,y; 15 }point; 16 17 point c[MAX]; 18 19 bool dy(double x,double y) 20 { 21 return x>y+eps; 22 } 23 bool xy(double x,double y) 24 { 25 return x<y-eps; 26 } 27 bool xyd(double x,double y) 28 { 29 return x<y+eps; 30 } 31 bool dyd(double x,double y) 32 { 33 return x>y-eps; 34 } 35 bool dd(double x,double y) 36 { 37 return fabs(x-y)<eps; 38 } 39 40 point stk[MAX]; 41 int top; 42 43 double crossProduct(point a,point b,point c) 44 { 45 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 46 } 47 double dist(point a,point b) 48 { 49 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 50 } 51 52 double dist1(point a,point b) 53 { 54 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); 55 } 56 57 bool cmp(point a,point b) 58 { 59 if(dd(a.y,b.y)) 60 { 61 return xy(a.x,b.x); 62 } 63 return xy(a.y,b.y); 64 } 65 bool cmp1(point a,point b) 66 { 67 double len=crossProduct(c[0],a,b); 68 if(dd(len,0.0)) 69 { 70 return xy(dist(c[0],a),dist(c[0],b)); 71 } 72 return xy(len,0.0); 73 } 74 75 double solve() 76 { 77 double maxx=0.0; 78 for(int i=0;i<=top;i++) 79 { 80 for(int j=i+1;j<=top;j++) 81 { 82 if(dy(dist1(stk[i],stk[j]),maxx)) 83 { 84 maxx=dist1(stk[i],stk[j]); 85 } 86 } 87 } 88 return maxx; 89 } 90 91 double Graham(int n) 92 { 93 sort(c,c+n,cmp); 94 sort(c+1,c+n,cmp1); 95 top=0; 96 stk[top++]=c[0]; 97 stk[top++]=c[1]; 98 stk[top++]=c[2]; 99 top--; 100 for(int i=3;i<n;i++) 101 { 102 while(1) 103 { 104 point a,b; 105 a=stk[top]; 106 b=stk[top-1]; 107 if(xyd(crossProduct(a,b,c[i]),0.0)) 108 { 109 top--; 110 } 111 else 112 break; 113 } 114 stk[++top]=c[i]; 115 } 116 return solve(); 117 } 118 119 int main() 120 { 121 int n,i,j; 122 while(scanf("%d",&n)!=EOF) 123 { 124 for(i=0;i<n;i++) 125 { 126 scanf("%lf%lf",&c[i].x,&c[i].y); 127 } 128 129 if(n==2) 130 { 131 printf("%.0lf\n",dist1(c[0],c[1])); 132 } 133 else 134 { 135 printf("%.0lf\n",Graham(n)); 136 } 137 } 138 return 0; 139 }
2014/7/27更新
旋转卡壳,卡了我两天啊,快卡死的节奏,照别人代码敲的,也不知道有没有漏掉什么,感觉凸包写的都有问题,仅供借鉴
再粘贴几个学习网址,感觉没几个人是真正理解的,包括我
http://blog.csdn.net/freezhanacmore/article/details/9527663
http://www.cnblogs.com/Booble/archive/2011/03/10/1980089.html
http://www.cnblogs.com/DreamUp/archive/2010/09/16/1828131.html
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7 #define eps 1e-6 8 #define MAX 50010 9 using namespace std; 10 typedef struct point 11 { 12 double x,y; 13 }point; 14 15 point c[MAX]; 16 int stk[MAX]; 17 int top; 18 19 bool xy(double x,double y){ return x<y-eps; } 20 bool dy(double x,double y){ return x>y+eps; } 21 bool xyd(double x,double y){ return x<y+eps; } 22 bool dyd(double x,double y){ return x>y-eps; } 23 bool dd(double x,double y){ return fabs(x-y)<eps; } 24 25 double crossProduct(point a,point b,point c) 26 { 27 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 28 } 29 double dist(point a,point b) 30 { 31 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 32 } 33 double dist_1(point a,point b) 34 { 35 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); 36 } 37 38 bool cmp(point a,point b) 39 { 40 double len=crossProduct(c[0],a,b); 41 if(dd(len,0.0)) 42 { 43 return xy(dist(c[0],a),dist(c[0],b)); 44 } 45 return xy(len ,0.0); 46 } 47 48 double max(double x,double y) 49 { 50 return xy(x,y)?y:x;// 51 } 52 double rotaing(int n) 53 { 54 int q=1; 55 double ans=0.0; 56 stk[n]=stk[0]; 57 for(int i=0;i<n;i++) 58 { 59 while( xy(fabs(crossProduct(c[stk[i]],c[stk[i+1]],c[stk[q]])), 60 fabs(crossProduct(c[stk[i]],c[stk[i+1]],c[stk[q+1]])))) 61 q=(q+1)%n; 62 ans=max(ans,dist_1(c[stk[i]],c[stk[q]])); 63 } 64 return ans; 65 } 66 67 double Graham(int n) 68 { 69 int tmp=0; 70 for(int i=1;i<n;i++) 71 { 72 if(xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y)) 73 tmp=i; 74 } 75 swap(c[0],c[tmp]); 76 sort(c+1,c+n,cmp); 77 stk[0]=0; 78 stk[1]=1; 79 top=1; 80 for(int i=2;i<n;i++) 81 { 82 while( xyd(crossProduct(c[stk[top]],c[stk[top-1]],c[i]),0.0)&&top>=1) 83 { 84 top--; 85 } 86 stk[++top]=i; 87 } 88 return rotaing(top+1); 89 } 90 91 int main() 92 { 93 int n,i,j,k,t; 94 while(scanf("%d",&n)!=EOF&&n) 95 { 96 for(i=0;i<n;i++) 97 { 98 scanf("%lf%lf",&c[i].x,&c[i].y); 99 } 100 int ans=(int )Graham(n); 101 printf("%d\n",ans); 102 } 103 return 0; 104 }