#include "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int crit_l, crit_r;
int cmp[1000000], cmp_l[1000000], cmp_r[1000000];
void extend(int &l, int &r) {
int ext_l = min(cmp_l[cmp[l]], cmp_l[cmp[r]]);
int ext_r = max(cmp_r[cmp[l]], cmp_r[cmp[r]]);
while (l != ext_l || r != ext_r) {
if (l != ext_l) {
l--;
ext_l = min(ext_l, cmp_l[cmp[l]]);
ext_r = max(ext_r, cmp_r[cmp[l]]);
} else {
r++;
ext_l = min(ext_l, cmp_l[cmp[r]]);
ext_r = max(ext_r, cmp_r[cmp[r]]);
}
}
}
ll compute(int l, int r) {
extend(l, r);
if (l == crit_l && r == crit_r) return 0;
if (l == crit_l) return compute(l, r + 1) + 2;
if (r == crit_r) return compute(l - 1, r) + 2;
ll to_l = 2;
int nl = l - 1, nr = r, tl = l, tr = r + 1;
extend(nl, nr), extend(tl, tr);
while (nl != crit_l && nr < tr) {
to_l += 2;
nl--;
extend(nl, nr);
}
if (nr < tr) return to_l + compute(nl, nr);
ll to_r = 2;
nl = l, nr = r + 1, tl = l - 1, tr = r;
extend(nl, nr), extend(tl, tr);
while (nl > tl) {
to_r += 2;
nr++;
extend(nl, nr);
}
return min(to_l, to_r) + compute(nl, nr);
}
ll minimum_walk(vector<int> p, int s) {
int n = p.size(), cmp_cnt = 0;
fill(cmp, cmp + n, -1);
crit_l = s, crit_r = s;
ll intra = 0;
for (int i = 0; i < n; i++) {
intra += abs(p[i] - i);
if (cmp[i] == -1) {
int curr = i;
cmp_l[cmp_cnt] = cmp_r[cmp_cnt] = i;
do {
cmp[curr] = cmp_cnt;
cmp_r[cmp_cnt] = max(cmp_r[cmp_cnt], curr);
curr = p[curr];
} while (curr != i);
if (i != p[i]) {
crit_l = min(crit_l, cmp_l[cmp_cnt]);
crit_r = max(crit_r, cmp_r[cmp_cnt]);
}
cmp_cnt++;
}
}
return intra + compute(s, s);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
213 ms |
12012 KB |
Output is correct |
31 |
Correct |
195 ms |
12140 KB |
Output is correct |
32 |
Correct |
133 ms |
19948 KB |
Output is correct |
33 |
Correct |
176 ms |
77548 KB |
Output is correct |
34 |
Correct |
185 ms |
77548 KB |
Output is correct |
35 |
Correct |
228 ms |
59116 KB |
Output is correct |
36 |
Correct |
143 ms |
26604 KB |
Output is correct |
37 |
Correct |
130 ms |
13036 KB |
Output is correct |
38 |
Correct |
134 ms |
12268 KB |
Output is correct |
39 |
Correct |
133 ms |
12268 KB |
Output is correct |
40 |
Correct |
140 ms |
12140 KB |
Output is correct |
41 |
Correct |
143 ms |
12012 KB |
Output is correct |
42 |
Correct |
144 ms |
12140 KB |
Output is correct |
43 |
Correct |
135 ms |
17900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
213 ms |
12012 KB |
Output is correct |
31 |
Correct |
195 ms |
12140 KB |
Output is correct |
32 |
Correct |
133 ms |
19948 KB |
Output is correct |
33 |
Correct |
176 ms |
77548 KB |
Output is correct |
34 |
Correct |
185 ms |
77548 KB |
Output is correct |
35 |
Correct |
228 ms |
59116 KB |
Output is correct |
36 |
Correct |
143 ms |
26604 KB |
Output is correct |
37 |
Correct |
130 ms |
13036 KB |
Output is correct |
38 |
Correct |
134 ms |
12268 KB |
Output is correct |
39 |
Correct |
133 ms |
12268 KB |
Output is correct |
40 |
Correct |
140 ms |
12140 KB |
Output is correct |
41 |
Correct |
143 ms |
12012 KB |
Output is correct |
42 |
Correct |
144 ms |
12140 KB |
Output is correct |
43 |
Correct |
135 ms |
17900 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
364 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
134 ms |
17900 KB |
Output is correct |
65 |
Correct |
166 ms |
18416 KB |
Output is correct |
66 |
Correct |
128 ms |
12268 KB |
Output is correct |
67 |
Correct |
135 ms |
12268 KB |
Output is correct |
68 |
Correct |
15 ms |
1772 KB |
Output is correct |
69 |
Correct |
13 ms |
1644 KB |
Output is correct |
70 |
Correct |
13 ms |
1772 KB |
Output is correct |
71 |
Correct |
14 ms |
2048 KB |
Output is correct |
72 |
Correct |
14 ms |
2156 KB |
Output is correct |
73 |
Correct |
15 ms |
1516 KB |
Output is correct |
74 |
Correct |
154 ms |
41452 KB |
Output is correct |
75 |
Correct |
190 ms |
47468 KB |
Output is correct |
76 |
Correct |
143 ms |
17772 KB |
Output is correct |
77 |
Correct |
165 ms |
17900 KB |
Output is correct |
78 |
Correct |
167 ms |
17004 KB |
Output is correct |
79 |
Correct |
140 ms |
17004 KB |
Output is correct |
80 |
Correct |
133 ms |
19936 KB |
Output is correct |
81 |
Correct |
154 ms |
47340 KB |
Output is correct |
82 |
Correct |
158 ms |
47428 KB |
Output is correct |
83 |
Correct |
144 ms |
17772 KB |
Output is correct |
84 |
Correct |
134 ms |
16492 KB |
Output is correct |
85 |
Correct |
156 ms |
13932 KB |
Output is correct |
86 |
Correct |
128 ms |
12652 KB |
Output is correct |
87 |
Correct |
179 ms |
33260 KB |
Output is correct |
88 |
Correct |
147 ms |
26252 KB |
Output is correct |
89 |
Correct |
145 ms |
19564 KB |
Output is correct |
90 |
Correct |
132 ms |
12356 KB |
Output is correct |
91 |
Correct |
132 ms |
12140 KB |
Output is correct |
92 |
Correct |
138 ms |
12012 KB |
Output is correct |