「Luogu P2742」【模板】二维凸包

给定一个点集,求它的凸包的周长。

链接

Luogu P2742

题解

二维凸包的模板题,没有什么好说的。
不会凸包的右转这里
但是最后求周长是一定要算点的距离,而不是向量的长度。被卡了两次

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef double db;
const ll MAXN=1e4+51;
struct Point{
db x,y;
Point(ll x=0,ll y=0)
{
this->x=x,this->y=y;
}
inline bool operator <(const Point &rhs)const
{
return y==rhs.y?x<rhs.x:y<rhs.y;
}
inline db polar()
{
return atan2(y,x);
}
};
typedef Point Vector;
Point p[MAXN];
ll cnt,minn;
db res;
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline Vector operator +(Vector x,Vector y)
{
return Vector(x.x+y.x,x.y+y.y);
}
inline Vector operator -(Vector x,Vector y)
{
return Vector(x.x-y.x,x.y-y.y);
}
inline Vector operator *(Vector x,db y)
{
return Vector(x.x*y,x.y*y);
}
inline Vector operator /(Vector x,db y)
{
return Vector(x.x/y,x.y/y);
}
inline db dot(Vector x,Vector y)
{
return x.x*y.x+x.y*y.y;
}
inline db length(Vector x)
{
return sqrt(dot(x,x));
}
inline db angle(Vector x,Vector y)
{
return acos(dot(x,y)/length(x)/length(y));
}
inline db cross(Vector x,Vector y)
{
return x.x*y.y-x.y*y.x;
}
inline db dist(Vector x,Vector y)
{
db xx=x.x-y.x,yy=x.y-y.y;
return sqrt(xx*xx+yy*yy);
}
inline bool cmp(Point x,Point y)
{
double xx=cross(x-p[1],y-p[1]);
if(xx>0)
{
return 1;
}
if(!xx&&length(x-p[1])<length(y-p[1]))
{
return 1;
}
return 0;
}
inline deque<Point> convexHull(Point *p,ll size)
{
Point top;
deque<Point>vec;
if(size==1)
{
vec.push_back(p[1]);
return vec;
}
if(size==2)
{
vec.push_back(p[1]),vec.push_back(p[2]);
return vec;
}
vec.push_back(p[1]),vec.push_back(p[2]);
for(register int i=3;i<=size;i++)
{
top=vec.back();
while(cross(top-vec[vec.size()-2],p[i]-top)<0)
{
vec.pop_back(),top=vec.back();
}
vec.push_back(p[i]);
}
return vec;
}
int main()
{
cnt=read();
p[0].x=p[0].y=100000000000.0;
if(cnt==0)
{
printf("0.00");
return 0;
}
for(register int i=1;i<=cnt;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
if(p[i]<p[minn])
{
minn=i;
}
}
swap(p[minn],p[1]),sort(p+2,p+cnt+1,cmp);
deque<Point>pt=convexHull(p,cnt);
if(pt.size()==1)
{
printf("%.2lf",0.0);
}
else
{
for(register int i=1;i<pt.size();i++)
{
res+=dist(pt[i],pt[i-1]);
}
res+=dist(pt[0],pt[pt.size()-1]);
printf("%.2lf",res);
}
}