#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 {
auto Expand = [&](int& l, int& r) {
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);
}
}
};
int l = s, r = s;
Expand(l, r);
int add = 0;
while (upd[r] != 0) {
--l, ++r;
++add;
Expand(l, r);
}
res += 2 * add;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 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 |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 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 |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
300 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 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 |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
300 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
155 ms |
20204 KB |
Output is correct |
31 |
Correct |
182 ms |
20248 KB |
Output is correct |
32 |
Correct |
80 ms |
8428 KB |
Output is correct |
33 |
Correct |
106 ms |
20172 KB |
Output is correct |
34 |
Correct |
98 ms |
20200 KB |
Output is correct |
35 |
Correct |
96 ms |
20156 KB |
Output is correct |
36 |
Correct |
96 ms |
20196 KB |
Output is correct |
37 |
Correct |
101 ms |
20200 KB |
Output is correct |
38 |
Correct |
100 ms |
20348 KB |
Output is correct |
39 |
Correct |
99 ms |
20204 KB |
Output is correct |
40 |
Correct |
101 ms |
20132 KB |
Output is correct |
41 |
Correct |
126 ms |
20248 KB |
Output is correct |
42 |
Correct |
133 ms |
20336 KB |
Output is correct |
43 |
Correct |
96 ms |
20248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 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 |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
300 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
155 ms |
20204 KB |
Output is correct |
31 |
Correct |
182 ms |
20248 KB |
Output is correct |
32 |
Correct |
80 ms |
8428 KB |
Output is correct |
33 |
Correct |
106 ms |
20172 KB |
Output is correct |
34 |
Correct |
98 ms |
20200 KB |
Output is correct |
35 |
Correct |
96 ms |
20156 KB |
Output is correct |
36 |
Correct |
96 ms |
20196 KB |
Output is correct |
37 |
Correct |
101 ms |
20200 KB |
Output is correct |
38 |
Correct |
100 ms |
20348 KB |
Output is correct |
39 |
Correct |
99 ms |
20204 KB |
Output is correct |
40 |
Correct |
101 ms |
20132 KB |
Output is correct |
41 |
Correct |
126 ms |
20248 KB |
Output is correct |
42 |
Correct |
133 ms |
20336 KB |
Output is correct |
43 |
Correct |
96 ms |
20248 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
296 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
304 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
304 KB |
Output is correct |
64 |
Correct |
91 ms |
26592 KB |
Output is correct |
65 |
Correct |
89 ms |
26600 KB |
Output is correct |
66 |
Correct |
92 ms |
26600 KB |
Output is correct |
67 |
Correct |
93 ms |
26596 KB |
Output is correct |
68 |
Correct |
10 ms |
2772 KB |
Output is correct |
69 |
Correct |
9 ms |
2816 KB |
Output is correct |
70 |
Correct |
9 ms |
2748 KB |
Output is correct |
71 |
Correct |
9 ms |
2816 KB |
Output is correct |
72 |
Correct |
9 ms |
2812 KB |
Output is correct |
73 |
Correct |
10 ms |
2772 KB |
Output is correct |
74 |
Correct |
91 ms |
26596 KB |
Output is correct |
75 |
Correct |
94 ms |
26596 KB |
Output is correct |
76 |
Correct |
93 ms |
26600 KB |
Output is correct |
77 |
Correct |
96 ms |
26596 KB |
Output is correct |
78 |
Correct |
104 ms |
26600 KB |
Output is correct |
79 |
Correct |
93 ms |
26596 KB |
Output is correct |
80 |
Correct |
74 ms |
14820 KB |
Output is correct |
81 |
Correct |
95 ms |
26604 KB |
Output is correct |
82 |
Correct |
83 ms |
26608 KB |
Output is correct |
83 |
Correct |
96 ms |
26600 KB |
Output is correct |
84 |
Correct |
93 ms |
26600 KB |
Output is correct |
85 |
Correct |
101 ms |
26596 KB |
Output is correct |
86 |
Correct |
93 ms |
26588 KB |
Output is correct |
87 |
Correct |
97 ms |
26612 KB |
Output is correct |
88 |
Correct |
93 ms |
26588 KB |
Output is correct |
89 |
Correct |
95 ms |
26596 KB |
Output is correct |
90 |
Correct |
92 ms |
26600 KB |
Output is correct |
91 |
Correct |
97 ms |
26612 KB |
Output is correct |
92 |
Correct |
102 ms |
26724 KB |
Output is correct |