POJ2187 Beauty Contest 水平序GrahamScan法

Posted legend_PawN

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2187 Beauty Contest 水平序GrahamScan法相关的知识,希望对你有一定的参考价值。

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 35550 Accepted: 11012

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




利用水平序的GrahamScan算法能够迅速建立凸包,然后进行暴力枚举,找到凸包中距离最远的两个点。
比较好的方法其实是旋转卡壳,由于数据不强,暴力的方法才能过

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=50000+10;
int n,top;
struct point
	int x,y;
	point() 
	point(int _x,int _y)
		x=_x;
		y=_y;
	
;
point operator - (point a,point b)
	return point(a.x-b.x,a.y-b.y);

int operator * (point a,point b)
	return a.x*b.x+a.y*b.y;

int operator ^ (point a,point b)
	return a.x*b.y-a.y*b.x;

bool cmp(point a,point b)
	return a.y<b.y || (a.y==b.y && a.x<b.x);

int dis(point a,point b)
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);

point p[maxn],stack[maxn];
void graham()
	if(n==1)
		top=1;
		stack[0]=p[0];
		return ;
	
	else if(n==2)
		top=2;
		stack[0]=p[0];stack[1]=p[1];
		return ;
	
	top=2;
	stack[0]=p[0];
	stack[1]=p[1];
	for(int i=2;i<n;i++)   //构造左链
		while(top>0 &&((stack[top-1]-stack[top-2])^(p[i]-stack[top-1]))<0)
			top--;
		stack[top++]=p[i];
	
	int temp=top;
	for(int i=n-2;i>=0;i--) //构造右链
		while(top>temp &&((stack[top-1]-stack[top-2])^(p[i]-stack[top-1]))<0)
			top--;
		stack[top++]=p[i];
	
	top--; //注意,最开始的点会重复

int solve()
	int res=0;
	sort(p,p+n,cmp);
	graham(); //水平序graham
	//for(int i=0;i<top;i++)  //加上注释可知凸包构建是否正确
	//	printf("(%d %d)\\n",stack[i].x,stack[i].y);
	//
	for(int i=0;i<top; ++i)
            for(int j=i+1; j < top; ++j)
               res=max(res,dis(stack[i],stack[j]));
	return res;

int main()
	//freopen("input.txt","r",stdin);
	while(~scanf("%d",&n))
		memset(stack,0,sizeof(stack));
		int ans;
		for(int i=0;i<n;i++)
			scanf("%d%d",&p[i].x,&p[i].y);
		ans=solve();
		printf("%d\\n",ans);
	




以上是关于POJ2187 Beauty Contest 水平序GrahamScan法的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2187 Beauty Contest

poj 2187 Beauty Contest——旋转卡壳

POJ - 2187 Beauty Contest(最远点对)

POJ2187 Beauty Contest (旋转卡壳算法 求直径)

POJ2187 Beauty Contest(旋转卡壳)

模板 旋转卡壳 poj2187