欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【POJ 2187】Beauty Contest

程序员文章站 2024-03-25 13:22:04
...

【题目】

传送门

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;
}
相关标签: 旋转卡壳