Beauty Contest 凸包+旋转卡壳法

Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 27507   Accepted: 8493

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

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 
 
 
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<math.h>
 4 #include<stdio.h>
 5 using namespace std;
 6 #define PI 3.1415926535898
 7 struct point
 8 {
 9     int x,y;
10 };
11 point p[60000],res[60000];
12 int n,top;
13 int Dist(const point &arg1, const point &arg2)
14 {
15     return (arg1.x - arg2.x)*(arg1.x - arg2.x) + (arg1.y - arg2.y)*(arg1.y - arg2.y);
16 }
17 bool multi(point p0,point p1,point p2)
18 {
19     return (p1.x-p0.x)*(p2.y-p0.y)>(p2.x-p0.x)*(p1.y-p0.y);
20 }
21 bool cmp(const point &a,const point &b)
22 {
23     point temp=p[0];
24     double xmt=(a.x-temp.x)*(b.y-temp.y)-(b.x-temp.x)*(a.y-temp.y);
25     if(xmt)                             //向量不共线就按逆时针旋转
26         return xmt>0;
27     return Dist(a,temp)>Dist(b,temp);//向量共线取最长的。
28 }
29 void graham()//p[0]是左下角的元素
30 {
31     res[0]=p[0];
32     sort(p+1,p+n,cmp);//按照极角排序
33     res[1]=p[1];
34     res[2]=p[2];
35     top=2;
36 
37     for(int i=3; i<n; i++)
38     {
39         while(top>0&&multi(p[i],res[top],res[top-1]))
40             top--;
41         res[++top]=p[i];
42     }
43 }
44 int cross(point a,point b,point o)
45 {
46     return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);
47 }
48 void rotating_calipers()
49 {
50     int q=1,ans=0;
51     res[++top]=res[0];
52     for(int p=0;p<n;p++)
53     {
54         while(cross(res[p+1],res[q+1],res[p])>cross(res[p+1],res[q],res[p]))
55             q=(q+1)%top;
56         ans=max(ans,max(Dist(res[p],res[q]),Dist(res[p+1],res[q+1])));
57     }
58     cout<<ans<<endl;
59 }
60 int main()
61 {
62     int i,mina,m;
63     point temp;
64     while(~scanf("%d",&n))
65     {
66         scanf("%d%d",&p[0].x,&p[0].y);
67         mina=0;
68         for(i=1; i<n; i++)
69         {
70             scanf("%d%d",&p[i].x,&p[i].y);
71             if(p[i].y<p[mina].y||(p[i].y==p[mina].y&&p[i].x<p[mina].x))
72                 mina=i;
73         }
74         swap(p[0].x,p[mina].x),swap(p[0].y,p[mina].y);
75         graham();
76         rotating_calipers();
77     }
78 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3885133.html