【POJ 2187】Beauty Contest

【题目】

传送门

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)

【分析】

大致题意:给出一个点集,求出最远的一对点,输出他们距离的平方

题解:旋转卡壳模板题

首先我们要知道,最远的点对肯定在凸包上(应该是很显然的吧)

想象有两条平行线,其中有一条与原凸包的边重合,另一条就在另一侧刚刚好交上凸包的位置(可以形象地理解成这两条线"卡"住了这个凸包),不断地转这两条线(转一圈),就能找到最远的一对点

(这样做应该是对的,但我不会这样做的证明,就感性理解一下吧)


【代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 50005
using namespace std;
int n,top;
struct point
{
	int x,y;
	point(){}
	point(int x,int y):x(x),y(y){}
	point operator+(const point &a)  {return point(x+a.x,y+a.y);}
	point operator-(const point &a)  {return point(x-a.x,y-a.y);}
	friend int dot(const point &a,const point &b)  {return a.x*b.x+a.y*b.y;}
	friend int cross(const point &a,const point &b)  {return a.x*b.y-a.y*b.x;}
	friend int dist(const point &a,const point &b)  {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
}a[N],sta[N],S;
bool comp(point p,point q)
{
	if(!cross(p-S,q-S))
	  return dot(p-S,p-S)<dot(q-S,q-S);
	return cross(p-S,q-S)>0;
} 
void Graham()
{
	int i;
	for(i=2;i<=n;++i)
	  if(a[1].x>a[i].x||(a[1].x==a[i].x&&a[1].y>a[i].y))
	    swap(a[1],a[i]);
	S=a[1];sta[++top]=a[1];
	sort(a+1,a+n+1,comp);
	for(i=2;i<=n;++i)
	{
		while(top>=2&&cross(a[i]-sta[top-1],sta[top]-sta[top-1])>=0)  top--;
		sta[++top]=a[i];
	}
}
int area(point x,point y,point z)
{
	return abs(cross(y-x,z-x));
}
int next(int x)
{
	if(x==top)
	  return 1;
	return x+1;
}
int solve()
{
	if(top==2)
	  return dist(sta[1],sta[2]);
	int i,j=3,ans=0;
	sta[top+1]=sta[1];
	for(i=1;i<=top;++i)
	{
		while(next(j)!=i&&area(sta[i],sta[i+1],sta[j])<=area(sta[i],sta[i+1],sta[j+1]))  j=next(j);
		ans=max(ans,dist(sta[j],sta[i]));
		ans=max(ans,dist(sta[j],sta[i+1]));
	}
	return ans;
}
int main()
{
	int i,ans;
	scanf("%d",&n);
	for(i=1;i<=n;++i)
	  scanf("%d%d",&a[i].x,&a[i].y);
	Graham();
	ans=solve();
	printf("%d",ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值