#include "books.h"
//author: erray
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/home/ioi/codes/debug.h"
#else
#define debug(...) (void) 37
#endif
long long minimum_walk(std::vector<int> P, int s) {
int N = int(P.size());
//assert(s == 0);
long long res = 0;
int start = 0, end = N - 1;
while (start < N && P[start] == start) {
++start;
}
while (end >= 0 && P[end] == end) {
--end;
}
if (start == N) {
return 0;
}
//res += 2 * start;
vector<int> upd(N);
for (int i = 0; i < N; ++i) {
int L = P[i], R = i;
if (L > R) {
swap(L, R);
}
upd[L] += 1;
upd[R] -= 1;
res += (R - L);
}
for (int i = 0; i < N - 1; ++i) {
upd[i + 1] += upd[i];
}
for (int i = start; i < end; ++i) {
if (upd[i] == 0) {
res += 2;
}
}
debug(upd, res);
vector<int> L(N, -1), R(N);
for (int i = 0; i < N; ++i) {
if (L[i] == -1) {
int v = i;
L[i] = R[i] = i;
do {
v = P[v];
L[i] = min(L[i], v);
R[i] = max(R[i], v);
} while (v != i);
do {
v = P[v];
L[v] = L[i];
R[v] = R[i];
} while (v != i);
}
}
debug(start, end, res, L, R);
if (s <= start) {
res += (start - s) * 2;
} else if (end <= s) {
res += (s - end) * 2;
} else {
int l = s, r = s;
int add = 0;
while (upd[r] != 0) {
debug(l, r);
--l, ++r;
++add;
int go_l = min(L[l], L[r]);
int go_r = max(R[l], R[r]);
while (go_l < l || go_r > r) {
while (go_l < l) {
--l;
go_l = min(L[l], go_l);
go_r = max(R[l], go_r);
}
while (go_r > r) {
++r;
go_l = min(L[r], go_l);
go_r = max(R[r], go_r);
}
}
}
res += 2 * add;
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
252 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
300 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
252 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
300 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
142 ms |
26596 KB |
Output is correct |
31 |
Correct |
171 ms |
26596 KB |
Output is correct |
32 |
Correct |
76 ms |
14808 KB |
Output is correct |
33 |
Correct |
90 ms |
26588 KB |
Output is correct |
34 |
Correct |
91 ms |
26472 KB |
Output is correct |
35 |
Correct |
94 ms |
26472 KB |
Output is correct |
36 |
Correct |
96 ms |
26600 KB |
Output is correct |
37 |
Correct |
92 ms |
26592 KB |
Output is correct |
38 |
Correct |
92 ms |
26596 KB |
Output is correct |
39 |
Correct |
90 ms |
26596 KB |
Output is correct |
40 |
Correct |
107 ms |
26596 KB |
Output is correct |
41 |
Correct |
119 ms |
26588 KB |
Output is correct |
42 |
Correct |
114 ms |
26588 KB |
Output is correct |
43 |
Correct |
93 ms |
26592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
300 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3306' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
252 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
300 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
142 ms |
26596 KB |
Output is correct |
31 |
Correct |
171 ms |
26596 KB |
Output is correct |
32 |
Correct |
76 ms |
14808 KB |
Output is correct |
33 |
Correct |
90 ms |
26588 KB |
Output is correct |
34 |
Correct |
91 ms |
26472 KB |
Output is correct |
35 |
Correct |
94 ms |
26472 KB |
Output is correct |
36 |
Correct |
96 ms |
26600 KB |
Output is correct |
37 |
Correct |
92 ms |
26592 KB |
Output is correct |
38 |
Correct |
92 ms |
26596 KB |
Output is correct |
39 |
Correct |
90 ms |
26596 KB |
Output is correct |
40 |
Correct |
107 ms |
26596 KB |
Output is correct |
41 |
Correct |
119 ms |
26588 KB |
Output is correct |
42 |
Correct |
114 ms |
26588 KB |
Output is correct |
43 |
Correct |
93 ms |
26592 KB |
Output is correct |
44 |
Incorrect |
1 ms |
300 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3306' |
45 |
Halted |
0 ms |
0 KB |
- |