#include "books.h"
#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool cmin(X &a, const Y &b) {
return a > b ? a = b, 1 : 0;
}
template <class X, class Y>
bool cmax(X &a, const Y &b) {
return a < b ? a = b, 1 : 0;
}
long long minimum_walk(vector<int> p, int s) {
int n = p.size(), cnt = 0;
long long res = 0;
vector<int> idx(n, -1), lef(n), rig(n);
int min_l = s, max_r = s;
for (int i = 0; i < n; i++) {
res += abs(p[i] - i);
if (idx[i] >= 0) continue;
lef[cnt] = rig[cnt] = i;
int j = i;
while (idx[j] < 0) {
idx[j] = cnt;
cmin(lef[cnt], j);
cmax(rig[cnt], j);
j = p[j];
}
cnt++;
if (i != p[i]) {
cmin(min_l, i);
cmax(max_r, i);
}
}
auto extend = [&](int &l, int &r) {
int tmp_l = min(lef[idx[l]], lef[idx[r]]);
int tmp_r = max(rig[idx[l]], rig[idx[r]]);
while (l > tmp_l || r < tmp_r) {
if (l > tmp_l)
cmin(tmp_l, lef[idx[l--]]);
if (r < tmp_r)
cmax(tmp_r, rig[idx[r++]]);
}
};
int cur_l = s, cur_r = s;
for (int i = 0; i < n; i++)
if (i != p[i]) {
cmin(min_l, i);
cmax(max_r, i);
}
while (cur_l > min_l || cur_r < max_r) {
extend(cur_l, cur_r);
bool flag = false;
int lef_l = cur_l, lef_r = cur_r;
int cost_l = 0, cost_r = 0;
while (lef_l > min_l) {
extend(--lef_l, lef_r);
cost_l += 2;
if (lef_r > cur_r) {
flag = true; break;
}
}
int rig_l= cur_l, rig_r = cur_r;
while (rig_r < max_r) {
extend(rig_l, ++rig_r);
cost_r += 2;
if (rig_l < cur_l) break;
}
if (!flag) res += cost_l + cost_r;
else res += min(cost_l, cost_r);
cur_l = min(lef_l, rig_l);
cur_r = max(lef_r, rig_r);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
236 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
236 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
133 ms |
19812 KB |
Output is correct |
31 |
Correct |
146 ms |
19808 KB |
Output is correct |
32 |
Correct |
100 ms |
19824 KB |
Output is correct |
33 |
Correct |
107 ms |
19808 KB |
Output is correct |
34 |
Correct |
105 ms |
19780 KB |
Output is correct |
35 |
Correct |
109 ms |
19824 KB |
Output is correct |
36 |
Correct |
107 ms |
19820 KB |
Output is correct |
37 |
Correct |
98 ms |
19824 KB |
Output is correct |
38 |
Correct |
97 ms |
19812 KB |
Output is correct |
39 |
Correct |
109 ms |
19808 KB |
Output is correct |
40 |
Correct |
104 ms |
19780 KB |
Output is correct |
41 |
Correct |
126 ms |
19808 KB |
Output is correct |
42 |
Correct |
127 ms |
19780 KB |
Output is correct |
43 |
Correct |
111 ms |
19820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
236 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
133 ms |
19812 KB |
Output is correct |
31 |
Correct |
146 ms |
19808 KB |
Output is correct |
32 |
Correct |
100 ms |
19824 KB |
Output is correct |
33 |
Correct |
107 ms |
19808 KB |
Output is correct |
34 |
Correct |
105 ms |
19780 KB |
Output is correct |
35 |
Correct |
109 ms |
19824 KB |
Output is correct |
36 |
Correct |
107 ms |
19820 KB |
Output is correct |
37 |
Correct |
98 ms |
19824 KB |
Output is correct |
38 |
Correct |
97 ms |
19812 KB |
Output is correct |
39 |
Correct |
109 ms |
19808 KB |
Output is correct |
40 |
Correct |
104 ms |
19780 KB |
Output is correct |
41 |
Correct |
126 ms |
19808 KB |
Output is correct |
42 |
Correct |
127 ms |
19780 KB |
Output is correct |
43 |
Correct |
111 ms |
19820 KB |
Output is correct |
44 |
Incorrect |
0 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3308' |
45 |
Halted |
0 ms |
0 KB |
- |