【模板】凸包

时间:2019-09-02
本文章向大家介绍【模板】凸包,主要包括【模板】凸包使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

开始挑起计几大梁了。

先开始学凸包,找了道凸包模板题。 poj1113

模板参考了kuangbin的。

 1 /*************************************************************************
 2     > File Name: hdu1348.cpp
 3 # File Name: hdu1348.cpp
 4 # Author : xiaobuxie
 5 # QQ : 760427180
 6 # Email:760427180@qq.com
 7 # Created Time: 2019年09月02日 星期一 20时47分27秒
 8  ************************************************************************/
 9 
10 #include<iostream>
11 #include<cstdio>
12 #include<map>
13 #include<cmath>
14 #include<cstring>
15 #include<set>
16 #include<queue>
17 #include<vector>
18 #include<algorithm>
19 using namespace std;
20 typedef long long ll;
21 #define inf 0x3f3f3f3f
22 #define pq priority_queue<int,vector<int>,greater<int> >
23 ll gcd(ll a,ll b){
24     if(a<b) return gcd(b,a);
25     return b==0?a:gcd(b,a%b);
26 }
27 
28 
29 const double pi=acos(-1.0);
30 const int N=1000+9;
31 struct point{
32     int x,y;
33 }a[N];
34 point p0;
35 int sta[N];
36 int top=0;
37 int cross(point p0,point p1,point p2){
38     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
39 }
40 double dis(point p1,point p2){
41     return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
42 }
43 bool cmp(point p1,point p2){
44     if(atan2(p1.y-p0.y,p1.x-p0.x)!=atan2(p2.y-p0.y,p2.x-p0.x)){
45         return (atan2(p1.y-p0.y,p1.x-p0.x))<(atan2(p2.y-p0.y,p2.x-p0.x));
46     }
47     return p1.x<p2.x;
48 }
49 void init(int n){
50     int i,k;
51     k=0;
52     scanf("%d%d",&a[0].x,&a[0].y);
53     p0=a[0];
54     for(i=1;i<n;++i){
55         scanf("%d %d",&a[i].x,&a[i].y);
56         if( (p0.y > a[i].y) || ((p0.y == a[i].y) && (p0.x > a[i].x))){
57             p0=a[i];
58             k=i;
59         }
60     }
61     a[k]=a[0];
62     a[0]=p0;
63     sort(a+1,a+n,cmp);
64 }
65 void graham(int n){
66     if(n==1){
67         top=0;
68         sta[0]=0;
69     }
70     if(n==2){
71         top=1;
72         sta[0]=0;
73         sta[1]=1;
74     }
75     if(n>2){
76         sta[0]=0; sta[1]=1; top=1;
77         for(int i=2;i<n;++i){
78             while(top>0 && cross(a[sta[top-1]],a[sta[top]],a[i])<=0) top--;
79             ++top;
80             sta[top]=i;
81         }
82     }
83 }
84 int main(){
85     int n,l;
86     while(~scanf("%d %d",&n,&l)){
87         init(n);
88         graham(n);
89         double ans=2*pi*l;
90         for(int i=0;i<top;++i) ans+=dis(a[sta[i]],a[sta[i+1]]);
91         ans+=dis(a[sta[0]],a[sta[top]]);
92         printf("%d\n",(int)(ans+0.5));
93     }
94     return 0;
95 }
View Code

原文地址:https://www.cnblogs.com/xiaobuxie/p/11449165.html