Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 27183 | Accepted: 8402 |
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
大致题意:
给定平面上的一些散点集,求最远两点距离的平方值。
解题思路:
别想着暴力枚举任意亮点距离找最大,行不通,想想三点共线吧!
平面上的散点集的最远的两点距离必然在这个散点集的凸包的某两个顶点上出现。
那么先求凸包,再枚举顶点距离就OK了。
问题是用栈构造凸包之前要对散点集进行排序,水平序我不说了,我没看懂,听说很简单。我用的是极坐标排序。
利用叉积的旋向 配合 比较排序 (如插入排序,冒泡,快排等)可以对极坐标排序,推荐用qsort,不要用冒泡之类的,太慢了,用Graham都是想快而已。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define M 51000
int top;
struct node {
double x,y;
}p[M],stack[M];
double L(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double multi(node a,node b,node c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int cmp(node a,node b)
{
if(multi(a,b,p[0])>0)
return 1;
if(multi(a,b,p[0])==0&&L(a,p[0])<L(b,p[0]))
return 1;
return 0;
}
void GS(node p[],node stack[],int n)
{
int i,k=0;
node temp;
for(i=0;i<n;i++)
{
if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x)))
k=i;
}
temp=p[0];
p[0]=p[k];
p[k]=temp;
sort(p+1,p+n,cmp);
top=2;
stack[0]=p[0],stack[1]=p[1],stack[2]=p[2];
for(i=3;i<n;i++)
{
while(top>1&&multi(p[i],stack[top],stack[top-1])>=0)
top--;
stack[++top]=p[i];
}
}
int main()
{
double t;
int n,i,m,j;
while(scanf("%d",&n)!=EOF&&n)
{ t=0;
memset(p,0,sizeof(p));
memset(stack,0,sizeof(stack));
for(i=0;i<n;i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
}
if(n==1)
{
printf("0\n");
continue;
}
if(n==2)
{
printf("%.0lf\n",L(p[0],p[1])*L(p[0],p[1]));
continue;
}
GS(p,stack,n);
stack[top+1]=stack[0];
for(i=0;i<=top;i++) //这里是把第一个给了栈顶的下一个,构成环路。
for(j=i;j<top+1;j++)
{
t=t>L(stack[i],stack[j])?t:L(stack[i],stack[j]);
}
if(n>2)
printf("%.0lf\n",t*t);
}
return 0;
}