PKU 2187 Beauty Contest 凸包+旋转卡壳

    技术2025-08-28  10

     

    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 

    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

     

    旋转卡壳+凸包

    #include<iostream> #include<algorithm> #include<cmath> #include<stdio.h> #include<cstdlib> using namespace std; const int N=50005; const double esp=1.0e-8; typedef struct{double x,y;}point; point p[N],t[N]; int dblcmp(double x) { if(fabs(x)<esp) return 0; return (x>0)?1:-1; } bool cmp(const point& e1,const point& e2) { return e1.y<e2.y||(e1.y==e2.y&&e1.x<e2.x); } double distances(point a,point b) { return sqrt( (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y) ); } double crossleft(point a,point b,point c) { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } double rotating_calipers(int m) { int q=1; double ans=0.0; t[m]=t[0]; for(int p=0;p<m;p++) { while(crossleft(t[p],t[p+1],t[q+1])>crossleft(t[p],t[p+1],t[q])) q=(q+1)%m; ans=max(ans,max(distances(t[p],t[q]),distances(t[p+1],t[q+1]))); } return ans*ans; } int Jarvis(int n) { int k,top=0; t[top++]=p[0]; t[top++]=p[1]; for(int i=2;i<n;i++) { while(top>1&&dblcmp( crossleft(t[top-2],t[top-1],p[i]) )<=0) top--; t[top++]=p[i]; } k=top; t[top++]=p[n-2]; for(int i=n-3;i>=0;i--) { while(top>k&&dblcmp( crossleft(t[top-2],t[top-1],p[i]) )<=0) top--; t[top++]=p[i]; } return top-1; } int main() { // freopen("1.txt","r",stdin); int n,m; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf %lf",&p[i].x,&p[i].y); sort(p,p+n,cmp); m=Jarvis(n); int ans=rotating_calipers(m); printf("%d/n",ans); return 0; }  

     

    最新回复(0)