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);
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值