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
USACO 2003 Fall
似乎是旋转卡壳模板。
任选一个一定在凸包上的点(横坐标或纵坐标最小的点)。
然后把这个点视为坐标原点,对其他点作向量,按照向量的角度排序,然后依次将点加入栈中,如果出现了下凸(新点减栈顶点与栈顶点减栈顶下一个点的叉积符号不对),那么退栈直到没有下凸再压栈。
附大爷博客 http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html
似乎是旋转卡壳模板。
任选一个一定在凸包上的点(横坐标或纵坐标最小的点)。
然后把这个点视为坐标原点,对其他点作向量,按照向量的角度排序,然后依次将点加入栈中,如果出现了下凸(新点减栈顶点与栈顶点减栈顶下一个点的叉积符号不对),那么退栈直到没有下凸再压栈。
附大爷博客 http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005;
int n,tp,s[N];
struct node
{
int x,y;
}p[N];
int dis(node a,node b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(node a,node b,node c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
bool cmp(node a,node b)
{
int t=cross(p[0],a,b);
if(t>0||(t==0&&dis(p[0],a)>dis(p[0],b)))
return 1;
return 0;
}
void graham()
{
s[0]=0;
s[1]=1;
tp=1;
for(int i=2;i<=n-1;i++)
{
while(tp&&cross(p[s[tp-1]],p[s[tp]],p[i])<0)
tp--;
s[++tp]=i;
}
tp++;
}
void rc()
{
int ans=dis(p[s[0]],p[s[1]]),q=1;
for(int i=0;i<=tp-1;i++)
{
while(abs(cross(p[s[(i+1)%tp]],p[s[i%tp]],p[s[(q+1)%tp]]))>abs(cross(p[s[(i+1)%tp]],p[s[i%tp]],p[s[q%tp]])))
q=(q+1)%tp;
ans=max(ans,max(dis(p[s[i%tp]],p[s[q]]),dis(p[s[(i+1)%tp]],p[s[q]])));
}
printf("%d\n",ans);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<=n-1;i++)
scanf("%d%d",&p[i].x,&p[i].y);
int u=0;
for(int i=1;i<=n-1;i++)
if(p[u].y>p[i].y||(p[u].y==p[i].y&&p[u].x>p[i].x))
u=i;
swap(p[u].x,p[0].x);
swap(p[u].y,p[0].y);
sort(p+1,p+n,cmp);
graham();
rc();
return 0;
}