-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvex_hull.cpp
76 lines (68 loc) · 2.19 KB
/
convex_hull.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// For a set of points on a plane, convex hull is the smallest convex polygon that contains all these points
// Graham Scan Algorithm (not optimal but simpler, better - Chan's algorithm runs in O(n*log(h)))
// TC: O(n*log(n))
///////////////////// start yanking ////////////////////
template<class T>
struct point {
T x, y;
};
template<class T>
bool scomp(const point<T> &a, const point<T> &b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
template<class T>
bool clockwise(const point<T> &a, const point<T> &b, const point<T> &c) {
return a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y) < 0;
}
template<class T>
bool anticlockwise(const point<T> &a, const point<T> &b, const point<T> &c) {
return a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y) > 0;
}
template<class T>
vector<point<T>> convex_hull(vector<point<T>> &points) {
vector<point<T>> ch;
if (points.size() == 1) {
return ch;
}
sort(points.begin(), points.end(), scomp<T>);
point<T> p1 = points[0], p2 = points[points.size()-1];
vector<point<T>> up = {p1}, down = {p1};
for (ll i = 1; i < points.size(); i++) {
if (i == points.size()-1 || clockwise(p1, points[i], p2)) {
while (up.size() >= 2 && !clockwise(up[up.size()-2], up[up.size()-1], points[i])) {
up.pop_back();
}
up.push_back(points[i]);
}
if (i == points.size()-1 || anticlockwise(p1, points[i], p2)) {
while(down.size() >= 2 && !anticlockwise(down[down.size()-2], down[down.size()-1], points[i])) {
down.pop_back();
}
down.push_back(points[i]);
}
}
for (ll i = 0; i < up.size(); i++) {
ch.push_back(up[i]);
}
for (ll i = down.size()-2; i > 0; i--) {
ch.push_back(down[i]);
}
return ch;
}
///////////////////// stop yanking /////////////////////
int main() {
ll n;
cin >> n;
vector<point<ll>> points(n);
for (ll i = 0; i < n; i++) {
cin >> points[i].x >> points[i].y;
}
auto ch = convex_hull(points);
for (auto &pt : ch) {
cout << pt.x << " " << pt.y << "\n";
}
return 0;
}