-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficLights.cpp
More file actions
48 lines (40 loc) · 1.05 KB
/
Copy pathTrafficLights.cpp
File metadata and controls
48 lines (40 loc) · 1.05 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
39
40
41
42
43
44
45
46
47
48
//Traffic Lights - https://cses.fi/problemset/task/1163/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int x, n;
cin >> x >> n;
set<int> positions = {0, x};
set<tuple<int, int, int>> diffs = {make_tuple(x, 0, x)};
for (int i = 0; i < n; i++) {
int p;
cin >> p;
positions.insert(p);
auto it = positions.upper_bound(p); //first larger than p
diffs.insert({*it - p, p, *it});
it = prev(prev(it)); //first smaller than p
diffs.insert({p - *it, *it, p});
while (true) {
auto [diff, left, right] = *prev(diffs.end());
if (*positions.upper_bound(left) == right) {
cout << diff << " ";
break;
}
diffs.erase(*prev(diffs.end()));
}
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}