POJ 2187 凸包旋转卡壳

题意:

求平面最远点对。输出最远距离的平方。

参考:

http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html

http://www.cppblog.com/staryjy/archive/2009/11/19/101412.html

View Code
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 
 8 #define N 50050
 9 
10 using namespace std;
11 
12 struct PO
13 {
14     int x,y;
15 }p[N];
16 
17 int stk[N],top,n;
18 
19 inline bool cmp(const PO &a,const PO &b)
20 {
21     if(a.x==b.x) return a.y<b.y;
22     else return a.x<b.x;
23 }
24 
25 inline int cross(const PO &o,const PO &a,const PO &b)
26 {
27     return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
28 }
29 
30 inline int get_dis2(const PO &a,const PO &b)
31 {
32     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
33 }
34 
35 inline void read()
36 {
37     for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
38     sort(p+1,p+1+n,cmp);
39 }
40 
41 inline void graham()
42 {
43     sort(p+1,p+1+n,cmp);
44     top=0;
45     stk[++top]=1; stk[++top]=2;
46     for(int i=3;i<=n;i++)
47     {
48         while(top>=2&&cross(p[stk[top-1]],p[stk[top]],p[i])<=0) top--; 
49         stk[++top]=i;
50     }
51     int num=top;
52     for(int i=n-1;i>=1;i--)
53     {
54         while(top>num&&cross(p[stk[top-1]],p[stk[top]],p[i])<=0) top--;
55         stk[++top]=i;
56     }
57 }
58 
59 inline int rotating_calipers()
60 {
61     int ans=0,q=2;
62     for(int i=1;i<top;i++)
63     {
64         while(cross(p[stk[i+1]],p[stk[q+1]],p[stk[i]])>cross(p[stk[i+1]],p[stk[q]],p[stk[i]]))
65         {
66             q=(q+1)%top;
67             if(q==0) q++;
68         }
69         ans=max(ans,max(get_dis2(p[stk[i]],p[stk[q]]),get_dis2(p[stk[i+1]],p[stk[q+1]])));
70     }
71     return ans;
72 }
73 
74 inline void go()
75 {
76     graham();
77     printf("%d\n",rotating_calipers());
78 } 
79 
80 int main()
81 {
82     while(scanf("%d",&n)!=EOF) read(),go();
83     return 0;
84 } 

表示和dyf神牛讲的一点都不一样。。

目测用旋转法解会恶心死。。

原文地址:https://www.cnblogs.com/proverbs/p/2912641.html