-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhillwalk.cpp
98 lines (90 loc) · 2.45 KB
/
hillwalk.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
#include <iostream>
#include <string>
#include <utility>
#include <sstream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;
#define INF 1000000000
#define LL_INF 4500000000000000000
#define LSOne(S) (S & (-S))
#define EPS 1e-9
#define A first
#define B second
#define mp make_pair
#define PI acos(-1.0)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
struct Hill {
int x1, y1, x2, y2, id;
bool operator<(Hill other) const {
if (x2 < other.x2) {
return (long long) (y2 - other.y1) * (long long) (other.x2 - other.x1) <
(long long) (other.y2 - other.y1) * (long long) (x2 - other.x1);
} else {
return (long long) (other.y2 - y1) * (long long) (x2 - x1) >
(long long) (y2 - y1) * (long long) (other.x2 - x1);
}
}
};
struct Point {
int x, y, hillId;
bool end;
bool operator==(Point other) const {
return hillId == other.hillId;
}
bool operator<(Point other) const {
return x != other.x ? x < other.x : y < other.y;
}
};
int main() {
freopen("hillwalk.in", "r", stdin);
freopen("hillwalk.out", "w", stdout);
int n; cin >> n;
Hill hills[n];
vector<Point> points;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
hills[i] = Hill{x1, y1, x2, y2, i};
points.push_back(Point{x1, y1, i, false});
points.push_back(Point{x2, y2, i, true});
}
sort(points.begin(), points.end());
set<Hill> curHills;
Hill curHill; curHill.x1 = -1;
int ans = 0;
for (Point p : points) {
if (curHill.x1 == -1) {
curHills.insert(hills[p.hillId]);
curHill = hills[p.hillId];
ans++;
} else {
if (p.end) {
if (p.hillId == curHill.id) {
auto it = curHills.find(curHill);
if (it == curHills.begin()) {
cout << ans << endl;
return 0;
}
curHill = *(--it);
ans++;
}
curHills.erase(hills[p.hillId]);
} else {
curHills.insert(hills[p.hillId]);
}
}
}
cout << ans << endl;
return 0;
}