POJ 2187 Beauty Contest

来源:互联网 发布:淘宝c店还可以赚到钱吗 编辑:程序博客网 时间:2024/05/21 21:33

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

40 00 11 11 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;}

0 0