poj 2187(凸包)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 32975 Accepted: 10232

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

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

题目大意:

求所有点中距离最远的两点的距离。

解题思路:

距离最远的两点一定是在凸包上,所以先求出凸包,时间复杂度为O(nlogn)

用Graham扫描法求出凸包

http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html

Graham算法中有关叉积的应用

http://www.cnblogs.com/codingmylife/archive/2012/09/18/2690401.html

之后在凸包上求出最大的两点距离,用暴力枚举的话时间复杂度为O(n^2)可能会超时,所以选择用旋转卡壳算法,时间复杂度为O(n)。

旋转卡壳算法

http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html

最后的总时间复杂度为O(nlogn)

代码如下:

#include<iostream> 
#include<stdio.h> 
#include<math.h> 
#include<string.h> 
#include<vector> 
#include<queue> 
#include<map> 
#include<stack> 
#include<queue> 
#include<algorithm> 
using namespace std; 
const int MAX=0x7fffffff; 
int min(int a,int b) 
{ 
    if(a<b)return a; 
    else return b; 
} 
int max(int a,int b) 
{ 
    if(a>b)return a; 
    else return b; 
}
int n;

struct point
{
	int x,y,flag;
	double an;
};
point p[5000005];
int len(int x1,int y1,int x2,int y2)
{
	return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
bool cmp(point a,point b)
{
	if(a.y<b.y||(a.y==b.y&&a.x<b.x))return true;
	return false;
}
int jud(point p1,point p2,point p3)
{
	return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}
int tubao()
{
	vector<int>v;
	v.push_back(0);
	v.push_back(1);
	//Graham扫描法
	for(int i=2;i<n;i++)//对所有点扫两遍,就可以不用对所有点按极角排序
	{
			while(jud(p[v[v.size()-2]],p[v[v.size()-1]],p[i])<=0&&v.size()>=2)
			{
				v.pop_back();
			}
			v.push_back(i);
	}
	int pos=v.size();
	for(int i=n-2;i>0;i--)
	{
		while(v.size()>pos&&jud(p[v[v.size()-2]],p[v[v.size()-1]],p[i])<=0)
			v.pop_back();
		v.push_back(i);
	}
	int sum=len(p[0].x,p[0].y,p[1].x,p[1].y);
	int top=v.size()-1,q=1;
	for(int i=0;i<top;i++)//旋转卡壳
	{
		while(abs(jud(p[v[(i+1)%top]],p[v[i%top]],p[v[q%top]]))<abs(jud(p[v[(i+1)%top]],p[v[i%top]],p[v[(q+1)%top]])))
			q=(q+1)%top;
		sum=max(sum,max(len(p[v[i%top]].x,p[v[i%top]].y,p[v[q%top]].x,p[v[q%top]].y),len(p[v[(i+1)%top]].x,p[v[(i+1)%top]].y,p[v[q%top]].x,p[v[q%top]].y)));
	}
	return sum;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
		}
		if(n==2)
		{
			printf("%d\n",len(p[0].x,p[0].y,p[1].x,p[1].y));
			continue;
		}
		sort(p,p+n,cmp);//对所有点按y由小到大排序,如果y相等按x由小到大排序
		printf("%d\n",tubao());
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值