Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 39350 | Accepted: 12212 |
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
题意:给一堆点,找出最远的点对距离。
思路:显然该点对在凸包上,那么先求出凸包。旋转卡壳的对踵点:如果过凸包上的两个点可以画一对平行直线,使凸包上的所有点都夹在两条平行线之间或落在平行线上,那么这两个点叫做一对对踵点,显然最远点对必然属于对踵点对集合,那么可以找出所有对踵点取距离最大那个,方法就是枚举每一条边a1-a2,找出ax,使得a1-a2-ax组成的三角形面积最大,这个可以通过叉积判断。又枚举的过程中面积的变化是一个凸函数,枚举下一条边时从ax开始就行了。凸包O(nlogn),旋转卡壳O(n)。
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxn = 1e5+3;
int n;
struct node{double x, y;}a[maxn], ans[maxn];
inline node operator-(node a, node b)
{
return node{a.x-b.x, a.y-b.y};
}
inline double operator*(node a, node b)
{
return a.x*b.y - a.y*b.x;
}
inline double dis(node a)
{
return a.x*a.x+a.y*a.y;
}
inline bool cmp(node x, node y)
{
if(fabs((x-a[1])*(y-a[1]))>0) return (x-a[1])*(y-a[1])>0;
return dis(x-a[1])<dis(y-a[1]);
}
inline double cha(node a, node b, node c)
{
return fabs((a-b)*(a-c))/2;
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; ++i) scanf("%lf%lf",&a[i].x,&a[i].y);
for(int i=2;i<=n;i++)
if(a[i].y<a[1].y||(a[i].y==a[1].y&&a[i].x<a[1].x))
swap(a[1],a[i]);
sort(a+2,a+n+1,cmp);
int top=2;ans[1]=a[1];ans[2]=a[2];
for(int i=3; i<=n; ++i)
{
while(top>1&&(a[i]-ans[top-1])*(ans[top]-ans[top-1])>=0) --top;
ans[++top]=a[i];
}
for(int i=0;i<top;i++)
ans[i] = ans[i+1];
n=top;
double imax = 0;
for(int i=0,j=2;i<n;i++)
{
while(cha(ans[i],ans[(i+1)%n],ans[j]) < cha(ans[i],ans[(i+1)%n],ans[(j+1)%n])) j=(j+1)%n;
imax = max(dis(ans[i]-ans[j]),imax);
}
printf("%.0f",imax);
return 0;
}