-
Notifications
You must be signed in to change notification settings - Fork 248
/
ConvexHull.cpp
107 lines (79 loc) · 2.65 KB
/
ConvexHull.cpp
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
/**********************************************************************************
Convex Hull [ Graham-Andrew method, O(NlogN) ]
Based on problem 638 from informatics.mccme.ru:
http://informatics.mccme.ru/mod/statements/view3.php?chapterid=638
Tested on problem 290 from informatics.mccme.ru:
http://informatics.mccme.ru/mod/statements/view3.php?id=&chapterid=290
**********************************************************************************/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define sqr(x) ((x) * (x))
const double pi = acos(-1.0);
struct point {
double x, y;
};
int n;
vector <point> p, hull;
double ans;
bool cmp(point a, point b) {
return (a.x < b.x || (a.x == b.x && a.y < b.y));
}
bool eq(point a, point b) {
return (a.x == b.x && a.y == b.y);
}
bool isCCW(point a, point b, point c) {
return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0;
}
void setConvexHull(vector <point> p, vector <point> &h) {
sort(p.begin(), p.end(), cmp);
p.erase(unique(p.begin(), p.end(), eq), p.end());
vector <point> up, down;
point head = p[0], tail = p.back();
up.push_back(head); down.push_back(head);
for (int i = 1; i < (int) p.size(); i++) {
if (i == (int) p.size() - 1 || !isCCW(tail, head, p[i])) {
while ( (int) up.size() >= 2 && isCCW(up[up.size() - 2], up.back(), p[i]) )
up.pop_back();
up.push_back(p[i]);
}
if (i == (int) p.size() - 1 || isCCW(tail, head, p[i])) {
while ( (int) down.size() >= 2 && !isCCW(down[down.size() - 2], down.back(), p[i]) )
down.pop_back();
down.push_back(p[i]);
}
}
h.clear();
for (int i = 0; i < (int) up.size(); i++)
h.push_back(up[i]);
for (int i = (int) down.size() - 2; i > 0; i--)
h.push_back(down[i]);
}
double dist(point a, point b) {
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
double getPerimeter(vector <point> p) {
double per = 0;
for (int i = 1; i < (int) p.size(); i++)
per += dist(p[i - 1], p[i]);
per += dist(p.back(), p[0]);
return per;
}
int main() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
point tmp;
scanf("%lf %lf", &tmp.x, &tmp.y);
p.push_back(tmp);
}
setConvexHull(p, hull);
ans = getPerimeter(hull);
printf("%.1lf", ans);
return 0;
}