poj 2187 Beauty Contest (计算几何)

Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34823 Accepted: 10772

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) 

Source

[Submit]   [Go Back]   [Status]   [Discuss]


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-8
#define N 50003
using namespace std;
int n,m,tot;
struct vector
{
    double x,y;
	vector(double X=0,double Y=0)
	{
		x=X,y=Y; 
	};
	
};
vector operator -(vector a,vector b)  {return vector(a.x-b.x,a.y-b.y);}
vector operator +(vector a,vector b)  {return vector(a.x+b.x,a.y+b.y);}
vector operator *(vector a,double b)  {return vector(a.x*b,a.y*b);}
vector operator /(vector a,double b)  {return vector(a.x/b,a.y/b);}
bool operator ==(vector a,vector b)
{
	return b.x==a.x&&b.y==a.y;
}
bool operator <(const vector &a,const vector &b)
{
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
typedef vector point;
point ch[N],p[N];
int dcmp(double x)
{
	if (fabs(x)<eps)  return 0;
	return x<0?-1:1;
}
double dot(vector a,vector b)//点积
{
	return a.x*b.x+a.y*b.y;
}
double cross(vector a,vector b)//叉积,叉积为0,两直线平行
{
	return a.x*b.y-a.y*b.x;
}
double len(vector a)//向量的模长
{
	return sqrt(dot(a,a));
}
double distl(point p,point a,point b)//点到直线的距离,利用平行四边形的有向面积/底边向量的模长,得到的高即为点到直线的距离
{
	vector v1,v2;
	v1=b-a; v2=p-a;
	return fabs(cross(v1,v2))/len(v1);
}
int convexhull(point* p,int n,point* ch)//Graham扫描法求凸包,把给定点包围在内,面积最小的凸多边形,注意该算法不能有重复的点,不能三点共线
{
	sort(p,p+n);//把点按照X坐标排序
	int k,i,m=0;//注意是从0开始存储的 
	for (int i=0;i<n;i++)//把p1,p2放到凸包中,从p3开始,当当前点在凸包前进方向的左边时,加入该点;否则弹出最后加入的点,直到新点在左边,这样就确定了下凸壳
	{
		while(m>1&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
		ch[m++]=p[i];
	}
	k=m;
	for (int i=n-2;i>=0;i--)//然后反向确定上凸壳,因为之前下凸壳中的点是不能删去的所以m>k
	{
		while(m>k&&dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
		ch[m++]=p[i];
	}
	if (n>1)  m--;
	return m;
}
double rotating_calipers(point* ch,int n)//旋转卡壳,被凸包上被一对平行直线卡住的点叫对踵点,最远点对一定在凸包的一对对踵点上
{
	if (n==1)  return 0;
	if (n==2)  return dot(ch[0]-ch[1],ch[0]-ch[1]);//如果只有两个点,那么就是两点的直线距离
	int now=1,i;
	double ans=0;
	ch[n]=ch[0];
	for (int i=0;i<n;i++)
	{
		while(dcmp(distl(ch[now],ch[i],ch[i+1])-distl(ch[now+1],ch[i],ch[i+1]))<=0)//最远点随着平行线的旋转是单调的,所以点不会来回移动
		  now=(now+1)%n;
		ans=max(ans,dot(ch[now]-ch[i],ch[now]-ch[i]));
		ans=max(ans,dot(ch[now]-ch[i+1],ch[now]-ch[i+1]));//找到与当前直线平行的最远点,用来更新答案
	}
	return ans;
}
int main()
{
	scanf("%d",&n);
	for (int i=0;i<n;i++)
	 scanf("%lf%lf",&p[i].x,&p[i].y);
	tot=convexhull(p,n,ch);
	printf("%.0lf\n",rotating_calipers(ch,tot));
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值