poj 2187 Beauty Contest(平面最远点)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 24431 | Accepted: 7459 |
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)
Source
旋转卡壳学习链接:
想法:
总结起来,问题解决步骤为:
1、用Graham's Scanning求凸包
2、用Rotating Calipers求凸包直径,也就找到了最远点对。
该算法的平均复杂度为O(nlogn) 。最坏的情况下,如果这n个点本身就构成了一个凸包,时间复杂度为O(n^2)。 旋转卡壳可以用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离等。虽然算法的思想不难理解,但是实现起来真的很容易让人“卡壳”。
逆向思考,如果qa,qb是凸包上最远两点,必然可以分别过qa,qb画出一对平行线。通过旋转这对平行线,我们可以让它和凸包上的一条边重合,如图中蓝色直线,可以注意到,qa是凸包上离p和qb所在直线最远的点。于是我们的思路就是枚举凸包上的所有边,对每一条边找出凸包上离该边最远的顶点,计算这个顶点到该边两个端点的距离,并记录最大的值。直观上这是一个O(n2)的算法,和直接枚举任意两个顶点一样了。但是注意到当我们逆时针枚举边的时候,最远点的变化也是逆时针的,这样就可以不用从头计算最远点,而可以紧接着上一次的最远点继续计算,于是我们得到了O(n)的算法。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <iostream> 5 #include <math.h> 6 using namespace std; 7 8 9 struct Point 10 { 11 int x,y; 12 Point(int _x = 0, int _y = 0) 13 { 14 x = _x; 15 y = _y; 16 } 17 Point operator -(const Point &b)const 18 { 19 return Point(x - b.x, y - b.y); 20 } 21 int operator ^(const Point &b)const 22 { 23 return x*b.y - y*b.x; 24 } 25 int operator *(const Point &b)const 26 { 27 return x*b.x + y*b.y; 28 } 29 void input() 30 { 31 scanf("%d%d",&x,&y); 32 } 33 }; 34 int dist2(Point a,Point b)//距离的平方!!!attention!! 35 { 36 return (a-b)*(a-b); 37 } 38 const int MAXN = 50010; 39 Point list[MAXN]; 40 int Stack[MAXN],top; 41 bool _cmp(Point p1,Point p2) 42 { 43 int tmp = (p1-list[0])^(p2-list[0]); 44 if(tmp > 0)return true; 45 else if(tmp == 0 && dist2(p1,list[0]) <= dist2(p2,list[0])) 46 return true; 47 else return false; 48 } 49 void Graham(int n)//求凸包 50 { 51 Point p0; 52 int k = 0; 53 p0 = list[0]; 54 for(int i = 1;i < n;i++) 55 if(p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x)) 56 { 57 p0 = list[i]; 58 k = i; 59 } 60 swap(list[0],list[k]); 61 sort(list+1,list+n,_cmp); 62 if(n == 1) 63 { 64 top = 1; 65 Stack[0] = 0; 66 return; 67 } 68 if(n == 2) 69 { 70 top = 2; 71 Stack[0] = 0; 72 Stack[1] = 1; 73 return; 74 } 75 Stack[0] = 0; 76 Stack[1] = 1; 77 top = 2; 78 for(int i = 2;i < n;i++) 79 { 80 while(top > 1 && ((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <= 0 ) 81 top--; 82 Stack[top++] = i; 83 } 84 } 85 86 //旋转卡壳,求两点间距离平方的最大值 87 int rotating_calipers(Point p[],int n) 88 { 89 int ans = 0; 90 Point v; 91 int cur = 1; 92 for(int i = 0;i < n;i++) 93 { 94 v = p[i]-p[(i+1)%n]; 95 while((v^(p[(cur+1)%n]-p[cur])) < 0)//寻找到国定边的最远的点 p[cur],然后更新距离!!! 96 cur = (cur+1)%n; 97 98 ans = max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+1)%n],p[(cur+1)%n])));//这里其实可以记录最远点对的坐标!! 99 } 100 return ans; 101 } 102 Point p[MAXN]; 103 104 int main() 105 { 106 int n; 107 while(scanf("%d",&n) == 1) 108 { 109 for(int i = 0;i < n;i++) 110 list[i].input(); 111 Graham(n); 112 for(int i = 0;i < top;i++) 113 { 114 p[i] = list[Stack[i]]; 115 } 116 printf("%d\n",rotating_calipers(p,top)) ; 117 } 118 return 0; 119 } 120