Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 35482 | Accepted: 10985 |
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
题目链接:http://poj.org/problem?id=2187
题目大意:求平面上n个点的最远点对的距离平方
题目分析:最远点对肯定在凸包上(反证法),求出凸包以后,对于本题,因为坐标是整数且范围很小,所以纯净凸包上的顶点很少,n^2枚举可过,也可利用旋转卡壳法,在O(n)的时间复杂度下解决,旋转卡壳可以当成用两条平行线卡住凸包,然后旋转两条平行线一周,这里只拿出一个对踵点在某条平行线上的情况,因此相当于枚举凸包上的每条边,其对踵点就是与该边所能组成三角形面积最大的那个点(直接叉积),然后分别算出对踵点到边的两个端点的距离取最大。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 5e4 + 5;
int n;
struct POINT {
int x, y;
}p[MAX], stk[MAX], base;
void getBase() {
scanf("%d %d", &p[0].x, &p[0].y);
base.x = p[0].x;
base.y = p[0].y;
int pos = 0;
for (int i = 1; i < n; i ++) {
scanf("%d %d", &p[i].x, &p[i].y);
if (p[i].y < base.y || (p[i].y == base.y && p[i].x < base.x)) {
base.x = p[i].x;
base.y = p[i].y;
pos = i;
}
}
swap(p[0], p[pos]);
}
int getDist(POINT p1, POINT p2) {
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
int getCross(POINT p0, POINT p1, POINT p2) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
bool cmp(POINT p1, POINT p2) {
if (getCross(base, p1, p2) == 0) {
return getDist(base, p1) < getDist(base, p2);
}
if (getCross(base, p1, p2) > 0) {
return true;
}
return false;
}
int main() {
scanf("%d", &n);
getBase();
sort(p + 1, p + n, cmp);
stk[0] = p[0];
stk[1] = p[1];
int top = 1;
for (int i = 0; i < n; i ++) {
while (top > 0 && getCross(stk[top - 1], stk[top], p[i]) <= 0) {
top --;
}
stk[++ top] = p[i];
}
int ans = getDist(stk[0], stk[1]);
int j = 2;
stk[++ top] = stk[0];
for (int i = 0; i < top; i ++) {
while (getCross(stk[i], stk[i + 1], stk[j]) < getCross(stk[i], stk[i + 1], stk[(j + 1) % top])) {
j = (j + 1) % top;
}
ans = max(ans, max(getDist(stk[i], stk[j]), getDist(stk[i + 1], stk[j])));
}
printf("%d\n", ans);
}