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 

题目大意

给你n个点,求n个点中距离最远的两个点,输出他俩距离的平方。

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) 

题解

旋转卡壳。但是我觉的这题的最大收获是学了重载运算符。

话说旋转卡壳其实就是凸包完,用一个O(n)的扫描就可以了。具体的细节其他神犇的博客写得是清清楚楚。

说重载运算符的作用还是很大的,像向量、叉积之类的用重载运算符可轻易搞定。有一个问题需要了解的:重载运算符的那段代码长得像函数,比如我重载了乘号*:

int operator*(dian a,dian b)
{
	return a.x*b.y-b.x*a.y;
}
只有在乘号两边均为结构体变量时(如我之后代码中的d[i],q[zz]等)*号才会调用以上函数。否则像这样的代码:a=x*y(a,x,y均为普通int变量),*号还是原来的用法,表示两个数求积。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int n,zz;
struct dian {int x,y;} d[50002],q[50002];
void init()
{
	scanf("%d",&n);
	int i;
	for(i=1;i<=n;i++)  scanf("%d%d",&d[i].x,&d[i].y);
}
//-----------------------------------------------------------------------------
dian operator-(dian a,dian b)
{
	dian t;
	t.x=a.x-b.x; t.y=a.y-b.y;
	return t;
}
int operator*(dian a,dian b)
{
	return a.x*b.y-b.x*a.y;
}
int dis(dian a,dian b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool operator<(dian a,dian b)
{
	int t=(a-d[1])*(b-d[1]);
	if(t==0) return dis(a,d[1])<dis(b,d[1]);
	if(t>0) return true;
	else return false;
}
//-----------------------------------------------------------------------------
void graham()//凸包 
{
	int i,t=1;
	for(i=1;i<=n;i++)
	   {if(d[i].y<d[t].y||(d[i].y==d[t].y&&d[i].x<d[t].x)) t=i;}
	swap(d[1],d[t]); sort(d+2,d+n+1);//极角排序 
	q[1]=d[1]; q[2]=d[2]; zz=2;
	for(i=3;i<=n;i++)
	   {while(zz>1&&(d[i]-q[zz-1])*(q[zz]-q[zz-1])>=0)
	       zz--;
	    q[++zz]=d[i];
	   }
}
//------------------------------------------------------------------------------
void rotating_c()//旋转卡壳 
{
	int i,w=2,ans=0;
	q[zz+1]=d[1];
	for(i=1;i<=zz;i++)
	   {while( (q[i+1]-q[i])*(q[w]-q[i])<(q[i+1]-q[i])*(q[w+1]-q[i])  )
	       w=w%zz+1;
	    ans=max(ans,max(dis(q[w+1],q[i+1]),dis(q[w],q[i])));
	     //ans=max(ans,dis(q[w],q[i]));
	   }
	printf("%d\n",ans);
} 
int main()
{
	init(); graham(); rotating_c();
	//for(int i=1;i<=zz;i++) printf("%d %d\n",q[i].x,q[i].y);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值