POJ 2187 Beauty Contest 凸包

Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 27276   Accepted: 8432

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.

Input

* Line 1: A single integer, N

* 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


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

#define M 50005
struct node
{
	double x,y;
}A[M],B[M];

double cmp(node a,node b)      //先按X排序,其次按Y排序
{
	if(a.x != b.x)
		return a.x < b.x;
	else
		return a.y < b.y;
}

double dis(node a,node b)    //计算两点之间的距离
{
	return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);
}

double chaji(node a,node b,node c)      //叉积,推断方向
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

int tubao(int n)
{
	sort(A,A+n,cmp);
	int m=0,i;
	for(i=0;i<n;i++)      //构建下凸包边
	{
		while(m>1 && chaji(B[m-2],B[m-1],A[i]) < 0)
			m--;
		B[m++]=A[i];
	}

	int k=m;
	for(i=n-2;i>=0;i--)     //构建上凸包边
	{
		while(m>k && chaji(B[m-2],B[m-1],A[i]) < 0)
			m--;
		B[m++]=A[i];
	}

	if(n>1)  m--;
	return m;
}

int main()
{
	int n;
	cin>>n;
	int i,j;
	for(i=0;i<n;i++)
		cin>>A[i].x>>A[i].y;

	double p=tubao(n);
	__int64 max=0,q;
	for(i=0;i<p;i++)
		for(j=i+1;j<p;j++)
		{
			q=dis(B[i],B[j]);
			if(max<q)   max=q;
		}
	printf("%I64d\n",max);

	return 0;
}


posted @ 2016-02-09 18:35  mengfanrong  阅读(168)  评论(0编辑  收藏  举报