# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
470131 |
2021-09-03T05:12:19 Z |
nhphucqt |
Safety (NOI18_safety) |
C++17 |
|
279 ms |
24672 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
int n, k, a[N];
long long addLef, addRig, res;
multiset<long long> lef, rig;
pair<long long, long long> segment[N];
long long trace[N];
long long getLef() {
return *lef.rbegin() + addLef;
}
long long getRig() {
return *rig.begin() + addRig;
}
void debug() {
for (long long x : lef) cerr << x + addLef << ' ';
for (long long x : rig) cerr << x + addRig << ' ';
cerr << '\n';
}
void checkTrace() {
long long sum = 0;
for (int i = 1; i <= n; ++i) {
assert(i==n || abs(trace[i]-trace[i+1]) <= k);
sum += abs(a[i] - trace[i]);
}
assert(sum == res);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef NHPHUCQT
freopen("demo.inp", "r", stdin);
freopen("demo.out", "w", stdout);
#endif
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
if ((lef.empty() && rig.empty()) || (getLef() <= a[i] && a[i] <= getRig())) {
lef.insert(a[i] - addLef);
rig.insert(a[i] - addRig);
}
else {
if (a[i] < getLef()) {
res += getLef() - a[i];
lef.insert(a[i]-addLef);
lef.insert(a[i]-addLef);
rig.insert(getLef() - addRig);
lef.erase(--lef.end());
}
else {
res += a[i] - getRig();
rig.insert(a[i]-addRig);
rig.insert(a[i]-addRig);
lef.insert(getRig() - addLef);
rig.erase(rig.begin());
}
}
segment[i] = {getLef(), getRig()};
addLef -= k;
addRig += k;
}
cout << res;
// for (int i = 1; i <= n; ++i) {
// cerr << segment[i].first << ' ' << segment[i].second << '\n';
// }
trace[n] = segment[n].first;
for (int i = n-1; i >= 1; --i) {
long long l = trace[i+1] - k;
long long r = trace[i+1] + k;
if (segment[i].second < l) {
trace[i] = l;
}
long long interLef = max(l, segment[i].first);
long long interRig = min(r, segment[i].second);
if (interLef <= interRig) {
trace[i] = interLef;
}
else {
if (l > segment[i].second) {
trace[i] = l;
}
else {
trace[i] = r;
}
}
}
// for (int i = 1; i <= n; ++i) {
// cerr << trace[i] << ' ';
// }
// cerr << '\n';
checkTrace();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
16468 KB |
Output is correct |
2 |
Correct |
254 ms |
23092 KB |
Output is correct |
3 |
Correct |
253 ms |
24092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
844 KB |
Output is correct |
29 |
Correct |
3 ms |
844 KB |
Output is correct |
30 |
Correct |
4 ms |
844 KB |
Output is correct |
31 |
Correct |
3 ms |
716 KB |
Output is correct |
32 |
Correct |
3 ms |
844 KB |
Output is correct |
33 |
Correct |
3 ms |
844 KB |
Output is correct |
34 |
Correct |
3 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
3 ms |
844 KB |
Output is correct |
35 |
Correct |
3 ms |
844 KB |
Output is correct |
36 |
Correct |
4 ms |
844 KB |
Output is correct |
37 |
Correct |
3 ms |
716 KB |
Output is correct |
38 |
Correct |
3 ms |
844 KB |
Output is correct |
39 |
Correct |
3 ms |
844 KB |
Output is correct |
40 |
Correct |
3 ms |
844 KB |
Output is correct |
41 |
Correct |
3 ms |
972 KB |
Output is correct |
42 |
Correct |
3 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
844 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
3 ms |
860 KB |
Output is correct |
46 |
Correct |
3 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
155 ms |
16468 KB |
Output is correct |
20 |
Correct |
254 ms |
23092 KB |
Output is correct |
21 |
Correct |
253 ms |
24092 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
3 ms |
844 KB |
Output is correct |
38 |
Correct |
3 ms |
844 KB |
Output is correct |
39 |
Correct |
4 ms |
844 KB |
Output is correct |
40 |
Correct |
3 ms |
716 KB |
Output is correct |
41 |
Correct |
3 ms |
844 KB |
Output is correct |
42 |
Correct |
3 ms |
844 KB |
Output is correct |
43 |
Correct |
3 ms |
844 KB |
Output is correct |
44 |
Correct |
3 ms |
972 KB |
Output is correct |
45 |
Correct |
3 ms |
716 KB |
Output is correct |
46 |
Correct |
3 ms |
844 KB |
Output is correct |
47 |
Correct |
4 ms |
844 KB |
Output is correct |
48 |
Correct |
3 ms |
860 KB |
Output is correct |
49 |
Correct |
3 ms |
844 KB |
Output is correct |
50 |
Correct |
279 ms |
24672 KB |
Output is correct |
51 |
Correct |
177 ms |
20480 KB |
Output is correct |
52 |
Correct |
107 ms |
19652 KB |
Output is correct |
53 |
Correct |
127 ms |
22892 KB |
Output is correct |
54 |
Correct |
136 ms |
23772 KB |
Output is correct |
55 |
Correct |
109 ms |
20064 KB |
Output is correct |
56 |
Correct |
121 ms |
22028 KB |
Output is correct |
57 |
Correct |
115 ms |
20164 KB |
Output is correct |
58 |
Correct |
112 ms |
17568 KB |
Output is correct |
59 |
Correct |
133 ms |
22340 KB |
Output is correct |