POJ 2187 Beauty Contest

来源:互联网 发布:怎么把js文件格式化 编辑:程序博客网 时间:2024/06/06 00:20

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) 

Source

USACO 2003 Fall

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

旋转卡壳~

先计算出凸包,然后对于每条边寻找离它最远的点,更新距离。

由于上一次的点一定在这一次的点的一侧,所以O(n)就可以求出来。


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespace std;#define lim 1e-8int n,tot;double ans; double sqr(double u){return u*u;}struct point{double x,y;point() {}point(double u,double v):x(u),y(v) {}friend point operator + (point u,point v){return point(u.x+v.x,u.y+v.y);}friend point operator - (point u,point v){return point(u.x-v.x,u.y-v.y);}friend double operator * (point u,point v){return u.x*v.y-u.y*v.x;}friend double operator / (point u,point v){return u.x*v.x+u.y*v.y;}friend bool operator == (point u,point v){return fabs(u.x-v.x)<lim && fabs(u.y-v.y)<lim;}friend bool operator != (point u,point v){return !(u==v);}friend bool operator < (point u,point v){if(fabs(u.y-v.y)<lim) return u.x<v.x;return u.y<v.y;}friend double dis2(point u){return sqr(u.x)+sqr(u.y);}friend void print(point u){printf("%.2lf %.2lf\n",u.x,u.y);}}a[50001],c[50001];bool cmp(point u,point v){if(fabs((v-a[1])*(u-a[1]))<lim) return dis2(u-a[1])<dis2(v-a[1]);return (u-a[1])*(v-a[1])>0;}int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%lf%lf",&a[i].x,&a[i].y);if(a[i]<a[1]) swap(a[i],a[1]);}sort(a+2,a+n+1,cmp);c[++tot]=a[1],c[++tot]=a[2];for(int i=3;i<=n;i++){while((c[tot]-c[tot-1])*(a[i]-c[tot-1])<lim && tot>1) tot--;c[++tot]=a[i];}c[tot+1]=a[1];int now=2;for(int i=1;i<=tot;i++){while((c[i+1]-c[i])*(c[now]-c[i])<(c[i+1]-c[i])*(c[now+1]-c[i])){now++;if(now==tot+1) now=1;}ans=max(ans,dis2(c[now]-c[i]));}printf("%d\n",(int)ans);return 0;}


1 0
原创粉丝点击