【模板】 旋转卡壳 poj2187

题目链接:https://vjudge.net/problem/POJ-2187

旋转卡壳模板题。参考了hzwer(http://hzwer.com/4224.html)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define eps 1e-8
 6 using namespace std;
 7 int n,top;
 8 const int N = 5e4+9;
 9 int sta[N];
10 struct Point{
11     double x,y;
12     Point operator - (const Point& b)const{
13         return (Point){x-b.x,y-b.y};
14     }
15     double operator ^ (const Point& b)const{
16         return x*b.y - b.x*y;
17     }
18 }p[N],p0; 
19 bool cmp(Point a,Point b){
20     if(atan2(a.y - p0.y,a.x - p0.x) != atan2(b.y - p0.y,b.x - p0.x)){
21         return atan2(a.y - p0.y,a.x - p0.x) < atan2(b.y - p0.y,b.x - p0.x);
22     }
23     return a.x < b.x;
24 }
25 double dis2(Point a,Point b){
26     return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
27 }
28 void init(){
29     int k=0;
30     scanf("%lf %lf",&p[0].x,&p[0].y);
31 //    cin>>p[0].x>>p[0].y;
32     p0 = p[0];
33     for(int i = 1;i<n;++i){
34         scanf("%lf %lf",&p[i].x,&p[i].y);
35 //        cin>>p[i].x>>p[i].y;
36         if( (p0.y > p[i].y) || (p0.y == p[i].y && p0.x > p[i].x)){
37             p0 = p[i];
38             k = i;
39         }
40     }
41     p[k] = p[0];
42     p[0] = p0;
43     sort(p+1,p+n,cmp);
44 }
45 void graham(){
46     if(n == 2){
47         top = 1;
48         sta[0] = 0;
49         sta[1] = 1;
50         return;
51     }
52     sta[0] = 0; sta[1] = 1; top = 1;
53     for(int i = 2;i<n;++i){
54         while( top>0 && ( (p[sta[top]] - p[sta[top-1]]) ^ (p[i]-p[sta[top-1]]) ) <= 0 ) --top;
55         sta[++top] = i;
56     }
57 }
58 void RC(){
59     sta[top+1] = sta[0];
60     int now = 1;
61     double ans = 0;
62     for(int i = 0;i<=top;++i){
63         while( ((p[sta[i+1]]-p[sta[i]])^(p[sta[now]] - p[sta[i]])) < ((p[sta[i+1]]-p[sta[i]])^(p[sta[now+1]]-p[sta[i]])) ){
64             ++now;
65             if(now == top+1) now = 0;
66         }
67         ans = max(ans,max(dis2(p[sta[i]],p[sta[now]]),dis2(p[sta[i+1]],p[sta[now]])));
68     }
69     printf("%d\n",(int)ans);
70 }
71 int main(){
72     scanf("%d",&n);
73     init();
74     graham();
75     RC();
76     return 0;
77 }
View Code

猜你喜欢

转载自www.cnblogs.com/xiaobuxie/p/11622679.html
今日推荐