-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathConvex Hull.cpp
51 lines (44 loc) · 1.4 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+5;
struct Point {
int x, y;
void read(){ scanf("%d %d", &x, &y); }
Point operator +(const Point& b) const { return Point{x+b.x, y+b.y}; }
Point operator -(const Point& b) const { return Point{x-b.x, y-b.y}; }
ll operator *(const Point& b) const { return (ll) x * b.y - (ll) y * b.x; }
bool operator <(const Point& b) const { return x == b.x ? y < b.y : x < b.x; }
void operator +=(const Point& b) { x += b.x; y += b.y; }
void operator -=(const Point &b) { x -= b.x; y -= b.y; }
void operator *=(const int k) { x *= k; y *= k; }
ll cross(const Point& b, const Point& c) const {
return (b - *this) * (c - *this);
}
};
int N, S;
Point P[maxN];
vector<Point> hull;
int main(){
scanf("%d", &N);
for(int i = 0; i < N; i++)
P[i].read();
sort(P, P+N);
for(int t = 0; t < 2; t++){
for(int i = 0; i < N; i++){
while((int) hull.size()-S >= 2){
Point P1 = hull[hull.size()-2];
Point P2 = hull[hull.size()-1];
if(P1.cross(P2, P[i]) <= 0) break;
hull.pop_back();
}
hull.push_back(P[i]);
}
hull.pop_back();
S = hull.size();
reverse(P, P+N);
}
printf("%d\n", S);
for(Point h : hull)
printf("%d %d\n", h.x, h.y);
}