#include <bits/stdc++.h>
#include "books.h"
using namespace std;
const int N = 1000005;
long long minimum_walk(vector<int> p, int s) {
int n = p.size();
long long res = 0;
for (int i = 0; i < n; ++i) res += abs(i - p[i]);
int ml = n - 1, mr = 0;
for (int i = 0; i < n; ++i) {
if (i != p[i]) ml = min(ml, i), mr = max(mr, i);
}
int cl = s, cr = s;
queue<int> qu; qu.push(s);
while (1) {
while (qu.size()) {
int u = qu.front(); qu.pop();
while (cl > p[u]) {
cl--, qu.push(cl);
}
while (cr < p[u]) {
cr++, qu.push(cr);
}
}
int l = cl, r = cr;
int dl = 0, dr = 0;
bool fl = 0, fr = 0;
queue<int> ql, qr;
while (1) {
if (l <= ml) break;
l--, dl++, ql.push(l);
while (ql.size()) {
int u = ql.front(); ql.pop();
while (l > p[u]) {
l--, ql.push(l);
}
if (p[u] > s) fl = 1;
}
if (fl) break;
}
while (1) {
if (r >= mr) break;
r++, dr++, qr.push(r);
while (qr.size()) {
int u = qr.front(); qr.pop();
while (r < p[u]) {
r++, qr.push(r);
}
if (p[u] < s) fr = 1;
}
if (fr) break;
}
if (fl && fr) {
res += 2 * min(dl, dr);
while (cl > l) qu.push(--cl);
while (cr < r) qu.push(++cr);
}
else {
res += 2 * (dl + dr); break;
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
416 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
544 KB |
Output is correct |
10 |
Correct |
2 ms |
544 KB |
Output is correct |
11 |
Correct |
2 ms |
544 KB |
Output is correct |
12 |
Correct |
2 ms |
544 KB |
Output is correct |
13 |
Correct |
3 ms |
544 KB |
Output is correct |
14 |
Correct |
2 ms |
544 KB |
Output is correct |
15 |
Correct |
2 ms |
544 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
416 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
544 KB |
Output is correct |
10 |
Correct |
2 ms |
544 KB |
Output is correct |
11 |
Correct |
2 ms |
544 KB |
Output is correct |
12 |
Correct |
2 ms |
544 KB |
Output is correct |
13 |
Correct |
3 ms |
544 KB |
Output is correct |
14 |
Correct |
2 ms |
544 KB |
Output is correct |
15 |
Correct |
2 ms |
544 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
4 ms |
620 KB |
Output is correct |
25 |
Correct |
3 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
416 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
544 KB |
Output is correct |
10 |
Correct |
2 ms |
544 KB |
Output is correct |
11 |
Correct |
2 ms |
544 KB |
Output is correct |
12 |
Correct |
2 ms |
544 KB |
Output is correct |
13 |
Correct |
3 ms |
544 KB |
Output is correct |
14 |
Correct |
2 ms |
544 KB |
Output is correct |
15 |
Correct |
2 ms |
544 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
4 ms |
620 KB |
Output is correct |
25 |
Correct |
3 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
200 ms |
12524 KB |
Output is correct |
31 |
Correct |
186 ms |
12708 KB |
Output is correct |
32 |
Correct |
140 ms |
12708 KB |
Output is correct |
33 |
Correct |
157 ms |
12708 KB |
Output is correct |
34 |
Correct |
180 ms |
12708 KB |
Output is correct |
35 |
Correct |
174 ms |
12708 KB |
Output is correct |
36 |
Correct |
162 ms |
12708 KB |
Output is correct |
37 |
Correct |
158 ms |
12708 KB |
Output is correct |
38 |
Correct |
157 ms |
12708 KB |
Output is correct |
39 |
Correct |
158 ms |
12708 KB |
Output is correct |
40 |
Correct |
176 ms |
12708 KB |
Output is correct |
41 |
Correct |
196 ms |
12708 KB |
Output is correct |
42 |
Correct |
167 ms |
12708 KB |
Output is correct |
43 |
Correct |
149 ms |
12708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12708 KB |
Output is correct |
2 |
Correct |
2 ms |
12708 KB |
Output is correct |
3 |
Correct |
2 ms |
12708 KB |
Output is correct |
4 |
Correct |
3 ms |
12708 KB |
Output is correct |
5 |
Correct |
2 ms |
12708 KB |
Output is correct |
6 |
Correct |
2 ms |
12708 KB |
Output is correct |
7 |
Correct |
3 ms |
12708 KB |
Output is correct |
8 |
Correct |
2 ms |
12708 KB |
Output is correct |
9 |
Correct |
2 ms |
12708 KB |
Output is correct |
10 |
Correct |
2 ms |
12708 KB |
Output is correct |
11 |
Correct |
2 ms |
12708 KB |
Output is correct |
12 |
Correct |
3 ms |
12708 KB |
Output is correct |
13 |
Correct |
2 ms |
12708 KB |
Output is correct |
14 |
Correct |
2 ms |
12708 KB |
Output is correct |
15 |
Correct |
2 ms |
12708 KB |
Output is correct |
16 |
Correct |
2 ms |
12708 KB |
Output is correct |
17 |
Correct |
3 ms |
12708 KB |
Output is correct |
18 |
Correct |
2 ms |
12708 KB |
Output is correct |
19 |
Correct |
2 ms |
12708 KB |
Output is correct |
20 |
Correct |
2 ms |
12708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
416 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
544 KB |
Output is correct |
10 |
Correct |
2 ms |
544 KB |
Output is correct |
11 |
Correct |
2 ms |
544 KB |
Output is correct |
12 |
Correct |
2 ms |
544 KB |
Output is correct |
13 |
Correct |
3 ms |
544 KB |
Output is correct |
14 |
Correct |
2 ms |
544 KB |
Output is correct |
15 |
Correct |
2 ms |
544 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
620 KB |
Output is correct |
24 |
Correct |
4 ms |
620 KB |
Output is correct |
25 |
Correct |
3 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
200 ms |
12524 KB |
Output is correct |
31 |
Correct |
186 ms |
12708 KB |
Output is correct |
32 |
Correct |
140 ms |
12708 KB |
Output is correct |
33 |
Correct |
157 ms |
12708 KB |
Output is correct |
34 |
Correct |
180 ms |
12708 KB |
Output is correct |
35 |
Correct |
174 ms |
12708 KB |
Output is correct |
36 |
Correct |
162 ms |
12708 KB |
Output is correct |
37 |
Correct |
158 ms |
12708 KB |
Output is correct |
38 |
Correct |
157 ms |
12708 KB |
Output is correct |
39 |
Correct |
158 ms |
12708 KB |
Output is correct |
40 |
Correct |
176 ms |
12708 KB |
Output is correct |
41 |
Correct |
196 ms |
12708 KB |
Output is correct |
42 |
Correct |
167 ms |
12708 KB |
Output is correct |
43 |
Correct |
149 ms |
12708 KB |
Output is correct |
44 |
Correct |
2 ms |
12708 KB |
Output is correct |
45 |
Correct |
2 ms |
12708 KB |
Output is correct |
46 |
Correct |
2 ms |
12708 KB |
Output is correct |
47 |
Correct |
3 ms |
12708 KB |
Output is correct |
48 |
Correct |
2 ms |
12708 KB |
Output is correct |
49 |
Correct |
2 ms |
12708 KB |
Output is correct |
50 |
Correct |
3 ms |
12708 KB |
Output is correct |
51 |
Correct |
2 ms |
12708 KB |
Output is correct |
52 |
Correct |
2 ms |
12708 KB |
Output is correct |
53 |
Correct |
2 ms |
12708 KB |
Output is correct |
54 |
Correct |
2 ms |
12708 KB |
Output is correct |
55 |
Correct |
3 ms |
12708 KB |
Output is correct |
56 |
Correct |
2 ms |
12708 KB |
Output is correct |
57 |
Correct |
2 ms |
12708 KB |
Output is correct |
58 |
Correct |
2 ms |
12708 KB |
Output is correct |
59 |
Correct |
2 ms |
12708 KB |
Output is correct |
60 |
Correct |
3 ms |
12708 KB |
Output is correct |
61 |
Correct |
2 ms |
12708 KB |
Output is correct |
62 |
Correct |
2 ms |
12708 KB |
Output is correct |
63 |
Correct |
2 ms |
12708 KB |
Output is correct |
64 |
Correct |
155 ms |
19480 KB |
Output is correct |
65 |
Correct |
183 ms |
26192 KB |
Output is correct |
66 |
Correct |
183 ms |
32760 KB |
Output is correct |
67 |
Correct |
169 ms |
38772 KB |
Output is correct |
68 |
Correct |
18 ms |
38772 KB |
Output is correct |
69 |
Correct |
29 ms |
38772 KB |
Output is correct |
70 |
Correct |
18 ms |
38772 KB |
Output is correct |
71 |
Correct |
19 ms |
38772 KB |
Output is correct |
72 |
Correct |
17 ms |
38772 KB |
Output is correct |
73 |
Correct |
18 ms |
38772 KB |
Output is correct |
74 |
Correct |
207 ms |
45740 KB |
Output is correct |
75 |
Correct |
185 ms |
52536 KB |
Output is correct |
76 |
Correct |
214 ms |
63308 KB |
Output is correct |
77 |
Correct |
177 ms |
70052 KB |
Output is correct |
78 |
Correct |
161 ms |
74140 KB |
Output is correct |
79 |
Correct |
156 ms |
80884 KB |
Output is correct |
80 |
Correct |
160 ms |
86296 KB |
Output is correct |
81 |
Correct |
227 ms |
94448 KB |
Output is correct |
82 |
Correct |
253 ms |
101116 KB |
Output is correct |
83 |
Correct |
165 ms |
106620 KB |
Output is correct |
84 |
Correct |
193 ms |
113396 KB |
Output is correct |
85 |
Correct |
183 ms |
120012 KB |
Output is correct |
86 |
Correct |
178 ms |
126752 KB |
Output is correct |
87 |
Correct |
214 ms |
133428 KB |
Output is correct |
88 |
Correct |
194 ms |
140372 KB |
Output is correct |
89 |
Correct |
202 ms |
147060 KB |
Output is correct |
90 |
Correct |
181 ms |
153664 KB |
Output is correct |
91 |
Correct |
168 ms |
160532 KB |
Output is correct |
92 |
Correct |
201 ms |
167404 KB |
Output is correct |