Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 29329 | Accepted: 9100 |
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
旋转卡壳模板题。
这道题就是要求凸包的直径。
旋转卡壳:
首先明白对踵点的概念:若凸包上的两个顶点能落在一对平行线上则称他们为对踵点
那么直径一定是对踵点之间的距离。
怎么求出每一对对踵点?
如果直接按照上图来写的话,代码会十分冗长。
那么换个方法来做,当平行线中的一条旋转到与凸包上的边重合时,他的对踵点与这条边构成的三角形面积一定是最大的!求面积直接叉乘就可以了!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point
{
int x,y;
}a[50005];
int tail,h[100005],n;
bool cmp(Point a,Point b)
{
if (a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int Cross(Point a,Point b,Point c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
void Graham()
{
sort(a+1,a+1+n,cmp);
tail=0;
for (int i=1;i<=n;i++)
{
while (tail>=2&&Cross(a[h[tail-2]],a[h[tail-1]],a[i])<=0)
tail--;
h[tail++]=i;
}
int tmp=tail;
h[tail++]=n-1;
for (int i=n-2;i;i--)
{
while (tail>tmp&&Cross(a[h[tail-2]],a[h[tail-1]],a[i])<=0)
tail--;
h[tail++]=i;
}
tail--;
}
int Getdis(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int Rotating_calipers()
{
int ans=0,q=1;
for (int p=0;p<tail;p++)
{
while (Cross(a[h[p]],a[h[p+1]],a[h[q+1]])>Cross(a[h[p]],a[h[p+1]],a[h[q]]))
q=(q+1)%tail;
ans=max(ans,Getdis(a[h[p]],a[h[q]]));
}
return ans;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
Graham();
cout<<Rotating_calipers()<<endl;
return 0;
}
感悟:
发现自己一直以来的凸包求法有漏洞,求完下凸包之后要记录当前凸包中点的个数,求上凸包的时候一定不能删去求好的下凸包的点,这样可以处理点在一条直线的情况!