Beauty Contest
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 27636 | Accepted: 8536 |
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
//AC代码
/*
题意:一个平面内,给出n个点,问哪两个点的距离最大
此题先用凸包求出最外围的点,然后可以暴力枚举但是此题POJ数据不大,所以O(n^2)不TLE
但是用旋转卡壳O(n)算法最好
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=50010;
using namespace std;
int sign(double x)
{
return (x>eps)-(x<-eps);
}
typedef struct Node
{
int x;
int y;
Node(const double &_x=0, const double &_y=0) : x(_x), y(_y) {}
void input()
{
scanf("%d%d",&x,&y);
}
void output()
{
cout<<x<<" "<<y<<endl;
}
} point;
point list[Max],stack[Max];
int n;
int top;
int cnt;
int curcnt;
int xmult(point p0,point p1,point p2)
{
return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int dmult(point p0,point p1,point p2)
{
return( (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y));
}
int Distance(point p1,point p2)// 返回两点之间欧氏距离
{
return((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
bool cmp(point p1,point p2)
{
int temp;
temp=xmult(list[0],p1,p2);
if(temp>0)
return true;
if(temp==0&&(Distance(list[0],p1)<Distance(list[0],p2)))
return true;
return false;
}
void convex_hull()//凸包模板
{
int i;
for(i=1; i<n; i++)
{
point temp;
if((list[i].y<list[0].y)||(list[i].y==list[0].y&&list[i].x<list[0].x))
swap(list[0],list[i]);
}
sort(list+1,list+n,cmp);
top=1;
stack[0]=list[0];
stack[1]=list[1];
for(i=2; i<n; i++)
{
while(top>=1&&xmult(stack[top-1],stack[top],list[i])<=0)
top--;
top++;
stack[top]=list[i];
}
}
int rotating_calipers()//旋转卡壳
{
int q=1,ans=0,p;
stack[++top]=stack[0];
for(p=0; p<top; p++)
{
while(xmult(stack[p],stack[p+1],stack[q+1])>xmult(stack[p],stack[p+1],stack[q]))
q=(q+1)%top;
ans=max(ans,max(Distance(stack[p],stack[q]),Distance(stack[p+1],stack[q+1])));
}
return ans;
}
int main()
{
int m,i,j;
int sum;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i<n; i++)
{
list[i].input();
}
convex_hull();
//cout<<"ok"<<endl;
sum=0;
/*for(i=0;i<=top;i++)//暴力枚举,时间大概是200-300MS
{
for(j=i+1;j<=top;j++)
{
sum=max(Distance(stack[i],stack[j]),sum);
}
}*/
//cout<<top<<endl;
sum=rotating_calipers();//旋转卡壳100++MS,但是如果数据大点差距会很明显
printf("%d\n",sum);
}
return 0;
}
/*
20
5521 -4185
157 3291
439 9868
4812 -2903
4993 5723
-9349 8008
-9267 -4272
-4861 -9500
6270 -4962
7394 -1099
-4349 8442
-1944 -491
5147 -5526
-2062 4624
-7775 8187
156 7746
-9646 -9686
1038 -2855
-9818 -4150
594 5175
答案:484066141
10
0 0
10000 0
1 100
2 199
9999 100
9998 199
100 -900
200 -1799
9800 -1799
9900 -900
答案:100000000
*/