-
Notifications
You must be signed in to change notification settings - Fork 0
/
convexHull.cpp
75 lines (61 loc) · 1.54 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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
#define FOR(i, b, e) for(int i = b; i <= e; i++)
#define SIZE(x) ((int)(x).size())
#define PB push_back
#define ST first
#define ND second
LL det(const PLL &a, const PLL &b, const PLL &c)
{
return a.ST * b.ND + b.ST * c.ND + c.ST * a.ND
- a.ND * b.ST - b.ND * c.ST - c.ND * a.ST;
}
vector<PLL> hull(vector<PLL> v)
{
if(SIZE(v) < 2)
return v;
sort(v.begin(), v.end());
vector<PLL> r = {v.front()};
int minLen = 1;
FOR(t, 1, 2)
{
for(auto &i : v)
if(i != r.back())
{
// zamienic '>= 0' na '> 0' zeby dla trzech wspoliniowych punktow
// na otoczce srodkowy tez byl zawarty w wyniku
while(SIZE(r) > minLen && det(r[SIZE(r) - 2], r.back(), i) >= 0)
r.pop_back();
r.push_back(i);
}
minLen = SIZE(r);
reverse(v.begin(), v.end());
r.pop_back();
}
// W wersji z '> 0' zeby dla wszystkich punktow na jednej linii
// srodkowe nie byly powielone dwukrotie trzeba dodac
/*
if(SIZE(r) == 2 * SIZE(v) - 2)
r.erase(r.begin() + SIZE(v), r.end());
*/
return r;
}
int main()
{
int n;
cin >> n;
VPLL v;
FOR(i, 1, n)
{
int x, y;
cin >> x >> y;
v.PB({x, y});
}
VPLL h = hull(v);
cout << SIZE(h) << endl;
for(auto i : h)
cout << i.ST << ' ' << i.ND << endl;
}