Beauty Contest (凸包+旋转卡壳模板题)

Beauty Contest

题意:求凸包上最大点对的距离

AC_Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cstring>
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn = 5e4+10;
10 const double eps = 1e-8;
11 
12 
13 typedef struct Point{
14     double x,y;
15     Point(double x=0,double y=0):x(x),y(y){}
16 }Vector;
17 
18 Vector operator + (const Vector &A, const Vector &B){ return Vector(A.x+B.x,A.y+B.y); }
19 Vector operator - (const Vector &A, const Vector &B){ return Vector(A.x-B.x,A.y-B.y); }
20 Vector operator * (const Vector &A, double p){ return Vector(A.x*p,A.y*p); }
21 Vector operator / (const Vector &A, double p){ return Vector(A.x/p,A.y/p); }
22 
23 bool operator <(const Point &A,const Point &B){
24     return A.x<B.x || (A.x==B.x && A.y<B.y);
25 }
26 
27 int dcmp(double x){
28     if( fabs(x)<eps ) return 0;
29     return x<0 ? -1 : 1;
30 }
31 
32 double Cross(const Vector &A, const Vector &B){
33     return A.x*B.y-A.y*B.x;
34 }
35 
36 double Dot(const Vector &A, const Vector &B){
37     return A.x*B.x+A.y*B.y;
38 }
39 
40 double Length(const Vector &A){
41     return Dot(A,A);
42 }
43 
44 double Area2(const Point &A, const Point &B, const Point &C){
45     return Cross(B-A,C-A);
46 }
47 
48 int ConvexHull(Point *p,int n,Point *ch){
49     sort(p,p+n);
50     int m=0;
51     for(int i=0;i<n;i++){
52         while( m>1 && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0 ) m--;
53         ch[m++]=p[i];
54     }
55     int k=m;
56     for(int i=n-2;i>=0;i--){
57         while( m>k && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
58         ch[m++]=p[i];
59     }
60     if( n>1 ) m--;
61     return m;
62 }
63 
64 Point p[maxn],ans[maxn];
65 
66 double RotatingCaliper(int m, Point *ch){
67     int q=1;
68     ch[m] = ch[0];
69     double ans = 0.0;
70     for(int p=0;p<m;p++){
71         while( fabs( Area2(ch[p],ch[p+1],ch[q+1]) ) > fabs( Area2(ch[p],ch[p+1],ch[q]) ) ) q=(q+1)%m;
72         ans = max(ans, max(Length(ch[q+1]-ch[p]), Length(ch[q+1]-ch[p+1])));
73         ans = max(ans, max(Length(ch[q]-ch[p]), Length(ch[q]-ch[p+1])));
74     }
75     return ans;
76 }
77 
78 int main()
79 {
80     int n;
81     while( ~scanf("%d",&n) ){
82         for(int i=0;i<n;i++){
83             int x,y; scanf("%d%d",&x,&y);
84             p[i] = Point(x,y);
85         }
86         int cnt = ConvexHull(p,n,ans);
87         double res = RotatingCaliper(cnt,ans);
88         printf("%.0f
",res);
89     }
90     return 0;
91 }
原文地址:https://www.cnblogs.com/wsy107316/p/13528346.html