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
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
Sample Input
1 2 3 4 5 |
4 0 0 0 1 1 1 1 0 |
Sample Output
1 |
2 |
Hint
Source
题意很简单,直接计算凸包,然后旋转卡壳算凸包的直径就可以了
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector> #define N 50005 using namespace std; struct P{ int x,y; P(){} P(int x,int y):x(x),y(y){} int det(P p) { return x*p.y-y*p.x; } int dot(P p) { return x*p.x+y*p.y; } P operator +(P p) { return P(x+p.x,y+p.y); } P operator -(P p) { return P(x-p.x,y-p.y); } } point[N]; bool cmp_x(const P& p,const P& q) { if (p.x!=q.x) return p.x<q.x; else return p.y<q.y; } int dist(P p,P q) { return (p-q).dot(p-q); } vector<P> convex_hull(P* ps,int n) { sort(ps,ps+n,cmp_x); int k=0; vector<P> qs(n*2); for (int i=0;i<n;i++) { while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--; qs[k++]=ps[i]; } for (int i=n-2,t=k;i>=0;i--) { while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--; qs[k++]=ps[i]; } qs.resize(k-1); return qs; } int Rotate_Calipers(P* ps,int nn) { vector<P> qs=convex_hull(ps,nn); int n=qs.size(); if (n==2) return dist(qs[0],qs[1]); int i=0,j=0; for (int k=0;k<n;k++) { if (!cmp_x(qs[i],qs[k])) i=k; if (cmp_x(qs[j],qs[k])) j=k; } int res=0; int si=i,sj=j; while(i!=sj||j!=si) { res=max(res,dist(qs[i],qs[j])); if ((qs[(i+1)%n]-qs[i]).det(qs[(j+1)%n]-qs[j])<0) { i=(i+1) % n; } else { j=(j+1) % n; } } return res; } int main() { int n; while(~scanf("%d",&n)) { for(int i =0 ;i < n;i ++) scanf("%d%d",&point[i].x,&point[i].y); printf("%d\n",Rotate_Calipers(point,n)); } return 0; } |