-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0587.cpp
More file actions
38 lines (30 loc) · 1.09 KB
/
Copy path0587.cpp
File metadata and controls
38 lines (30 loc) · 1.09 KB
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
class Solution {
using point_t = vector<int>;
public:
vector<point_t> outerTrees(vector<point_t> &trees) const {
const auto cross = [](const point_t &a, const point_t &b, const point_t &c) {
return (c[1] - a[1]) * (b[0] - a[0]) - (c[0] - a[0]) * (b[1] - a[1]);
};
const auto cmp = [](const point_t &a, const point_t &b) {
return a[0] != b[0] ? a[0] < b[0] : a[1] < b[1];
};
const int n = size(trees);
vector<point_t> haul(2 * n);
int k = 0;
sort(begin(trees), end(trees), cmp);
for (int i = 0; i < n; i++) {
while (k >= 2 && cross(haul[k - 2], haul[k - 1], trees[i]) < 0)
k--;
haul[k++] = trees[i];
}
for (int i = n - 2, t = k + 1; i >= 0; i--) {
while (k >= t && cross(haul[k - 2], haul[k - 1], trees[i]) < 0)
k--;
haul[k++] = trees[i];
}
haul.resize(k);
sort(begin(haul), end(haul));
haul.erase(unique(begin(haul), end(haul)), end(haul));
return haul;
}
};