#include <bits/stdc++.h>
#include "books.h"
using namespace std;
using LL = long long;
#ifdef LORENZO
const int N = 1000;
#else
const int N = 1e6 + 42;
#endif
int t[2*N];
void upd(int l, int r) {
for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
if (l & 1) t[l++] = 1;
if (r & 1) t[--r] = 1;
}
}
int qry(int p) {
int ans = 0;
for (p += N; p > 0; p >>= 1) {
ans = max(ans, t[p]);
}
return ans == 0;
}
LL minimum_walk(vector<int> p, int s) {
int n = p.size();
LL ans = 0;
for (int i = 0; i < n; ++i) {
ans += abs(i - p[i]);
}
if (ans == 0) return ans;
int rl = s+1;
int rr = s;
vector<int> cyc(n, -1);
for (int i = 0, c = 0; i < n; ++i) {
if (cyc[i] != -1) continue;
int ii = i, l = i, r = i;
do {
cyc[ii] = c;
ii = p[ii];
l = min(l, ii);
r = max(r, ii);
} while (i != ii);
++c;
if (l != r) {
upd(l+1, r+1);
rl = min(rl, r+1);
rr = max(rr, l);
}
}
for (int i = rl; i <= rr; ++i) {
ans += 2 * qry(i);
}
// int first_step = 0;
// for (int d = 0;; ++d) {
// if (s-d >= 0 && p[s-d] != s-d) {
// first_step = d;
// break;
// } else if (s+d < n && p[s+d] != s+d) {
// first_step = d;
// break;
// }
// }
// ans += 2 * first_step;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
168 ms |
12164 KB |
Output is correct |
31 |
Correct |
156 ms |
12016 KB |
Output is correct |
32 |
Correct |
104 ms |
8048 KB |
Output is correct |
33 |
Correct |
130 ms |
19324 KB |
Output is correct |
34 |
Correct |
129 ms |
19276 KB |
Output is correct |
35 |
Correct |
128 ms |
19452 KB |
Output is correct |
36 |
Correct |
145 ms |
19696 KB |
Output is correct |
37 |
Correct |
131 ms |
19788 KB |
Output is correct |
38 |
Correct |
120 ms |
18932 KB |
Output is correct |
39 |
Correct |
133 ms |
18552 KB |
Output is correct |
40 |
Correct |
133 ms |
15012 KB |
Output is correct |
41 |
Correct |
144 ms |
12624 KB |
Output is correct |
42 |
Correct |
130 ms |
12928 KB |
Output is correct |
43 |
Correct |
134 ms |
19824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
168 ms |
12164 KB |
Output is correct |
31 |
Correct |
156 ms |
12016 KB |
Output is correct |
32 |
Correct |
104 ms |
8048 KB |
Output is correct |
33 |
Correct |
130 ms |
19324 KB |
Output is correct |
34 |
Correct |
129 ms |
19276 KB |
Output is correct |
35 |
Correct |
128 ms |
19452 KB |
Output is correct |
36 |
Correct |
145 ms |
19696 KB |
Output is correct |
37 |
Correct |
131 ms |
19788 KB |
Output is correct |
38 |
Correct |
120 ms |
18932 KB |
Output is correct |
39 |
Correct |
133 ms |
18552 KB |
Output is correct |
40 |
Correct |
133 ms |
15012 KB |
Output is correct |
41 |
Correct |
144 ms |
12624 KB |
Output is correct |
42 |
Correct |
130 ms |
12928 KB |
Output is correct |
43 |
Correct |
134 ms |
19824 KB |
Output is correct |
44 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |