POJ 2187 平面最远点对

来源:互联网 发布:php mysql 免费空间 编辑:程序博客网 时间:2024/06/03 12:35
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 26596 Accepted: 8202

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

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)

求平面最远点对的距离,由于poj数据比较水,也可以直接凸包暴力,采用旋转卡壳效率比较高。

代码:

/* ***********************************************Author :rabbitCreated Time :2014/4/20 9:39:31File Name :8.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-10#define pi acos(-1.0)typedef long long ll;int dcmp(double x){if(fabs(x)<eps)return 0;return x>0?1:-1;}struct Point{double x,y;Point(double _x=0,double _y=0){x=_x;y=_y;}};Point operator + (Point a,Point b){return Point(a.x+b.x,a.y+b.y);}Point operator - (Point a,Point b){return Point(a.x-b.x,a.y-b.y);}Point operator * (Point a,double p){return Point(a.x*p,a.y*p);}Point operator / (Point a,double p){return Point(a.x/p,a.y/p);}bool operator < (const Point &a,const Point &b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}bool operator == (const Point &a,const Point &b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}double Dot(Point a,Point b){return a.x*b.x+a.y*b.y;}double Length(Point a){return sqrt(Dot(a,a));}double Angle(Point a,Point b){return acos(Dot(a,b)/Length(a)/Length(b));}double angle(Point a){return atan2(a.y,a.x);}double Cross(Point a,Point b){return a.x*b.y-a.y*b.x;}Point vecunit(Point x){return x/Length(x);}Point Normal(Point x){return Point(-x.y,x.x);}Point Rotate(Point a,double rad){return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}struct Line{Point p,v;double ang;Line(){}Line(Point P,Point v):p(P),v(v){ang=atan2(v.y,v.x);}bool operator < (const Line &L) const {return ang<L.ang;}Point point(double a){return p+(v*a);}};Point p[50010],ch[50010];int ConvexHull(Point *p,int n,Point *ch){sort(p,p+n);int m=0;for(int i=0;i<n;i++){while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;ch[m++]=p[i];}int k=m;for(int i=n-2;i>=0;i--){while(m>k&&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 diameter(Point *ch,int m){if(m==1)return 0;if(m==2)return Length(ch[1]-ch[0]);ch[m]=ch[0];double ans=0;for(int u=0,v=1;u<m;u++){while(1){double diff=Cross(ch[u+1]-ch[u],ch[v+1]-ch[v]);if(diff<=0){ans=max(ans,Length(ch[u]-ch[v]));if(dcmp(diff)==0)ans=max(ans,Length(ch[u]-ch[v+1]));break;}v=(v+1)%m;}}return ans;}int main(){int n;while(~scanf("%d",&n)){for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);int m=ConvexHull(p,n,ch);double gg=diameter(ch,m);gg=gg*gg;double pp=gg-(int)gg;int ans=(int)gg;if(pp>=0.5)ans++;printf("%d\n",ans);}return 0;}


0 0
原创粉丝点击