Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 23413 | Accepted: 7144 |
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
题目大意:
给出N个点,求出最远两个点的距离的平方
先说说测评情况(POJ)
WA了无数次,原因很多,第一次写很多手残的地方,但是都改了以后还是WA,最终GDB单步才发现叉乘写错了!!!还是计算几何的基本知识不熟悉啊!!!!
其实思路没什么,就是旋转卡壳的基础联系题目,只是我这个弱菜不知道WA了几次啊。。。。。。
不过还是说说值得注意的几点
1.主函数中调用的graham()求出的凸包存在 P [ 0..Cnt-1 ] 中,并且 P[Cnt-1]=P[0],所以下面旋转的时候我们要写成 < Cnt-1
2.cross函数值得好好推敲一下(我也没怎么搞透彻,这一题调了一上午,现在去看看基本知识)
3.63行左右,那里的cross函数是加了abs的,可能是我的带参数的顺序或者是我的cross函数写挂了。。。。但是无所谓啦,反正那里是求面积~
C++ AC Code
/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
#define sqr(_) ((_)*(_))
const int N=50000+10;
int n;
struct node{int x,y;}a[N];
int st[N],top;
int P[N],Cnt;
int cross(int i,int j,int k)
{
int x1=a[j].x-a[i].x, y1=a[j].y-a[i].y;
int x2=a[k].x-a[i].x, y2=a[k].y-a[i].y;
return x1*y2-x2*y1;
}
bool cmp(node a,node b)
{
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
void graham()
{
sort(a+1,a+1+n,cmp);
st[top=1]=1;
for(int i=2;i<=n;i++)
{
while(top>1 && cross(st[top],st[top-1],i)<=0) top--;
st[++top]=i;
}
for(int i=1;i<=top;i++) P[Cnt++]=st[i];
st[top=1]=n;
for(int i=n-1;i>=1;i--)
{
while(top>1 && cross(st[top],st[top-1],i)<=0) top--;
st[++top]=i;
}
for(int i=2;i<=top;i++) P[Cnt++]=st[i];
//for(int i=0;i<Cnt;i++) printf("%d %d\n",a[P[i]].x,a[P[i]].y);
}
int dis(int i,int j)
{ return sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y); }
int main()
{
freopen("poj2187.in","r",stdin);
freopen("poj2187.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
graham();
int j=1,res=0;
for(int i=0;i<Cnt-1;i++)//"Cnt-1" that is because P[Cnt-1]=P[0]
{
while(abs(cross(P[i],P[j],P[i+1]))<abs(cross(P[i],P[j+1],P[i+1])))
if((++j)>=Cnt-1) j=0;//the same , "Cnt-1"
res=max(res,dis(P[i],P[j]));
}
printf("%d\n",res);
return 0;
}