#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];
map<pair<int, int>, ll> dp;
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 (dp.count({l, r})) return dp[{l, r}];
if (l == crit_l) return dp[{l, r}] = compute(l, r + 1) + 2;
if (r == crit_r) return dp[{l, r}] = compute(l - 1, r) + 2;
ll to_l = 2, to_r = 2;
int nl, nr, tl, tr;
nl = l - 1, nr = r, tl = l, tr = r + 1;
extend(nl, nr), extend(tl, tr);
while (nr < tr) {
to_l += 2;
nl--;
extend(nl, nr);
}
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++;
}
}
dp[{crit_l, crit_r}] = 0;
return intra + compute(s, s);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 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 |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 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 |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 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 |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 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 |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
190 ms |
12012 KB |
Output is correct |
31 |
Correct |
198 ms |
12032 KB |
Output is correct |
32 |
Correct |
136 ms |
19912 KB |
Output is correct |
33 |
Correct |
361 ms |
127596 KB |
Output is correct |
34 |
Correct |
369 ms |
127724 KB |
Output is correct |
35 |
Correct |
299 ms |
94852 KB |
Output is correct |
36 |
Correct |
186 ms |
37100 KB |
Output is correct |
37 |
Correct |
139 ms |
13636 KB |
Output is correct |
38 |
Correct |
137 ms |
12356 KB |
Output is correct |
39 |
Correct |
138 ms |
12228 KB |
Output is correct |
40 |
Correct |
139 ms |
12100 KB |
Output is correct |
41 |
Correct |
201 ms |
12100 KB |
Output is correct |
42 |
Correct |
151 ms |
12100 KB |
Output is correct |
43 |
Correct |
191 ms |
17900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
Runtime error |
6 ms |
492 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 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 |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 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 |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
190 ms |
12012 KB |
Output is correct |
31 |
Correct |
198 ms |
12032 KB |
Output is correct |
32 |
Correct |
136 ms |
19912 KB |
Output is correct |
33 |
Correct |
361 ms |
127596 KB |
Output is correct |
34 |
Correct |
369 ms |
127724 KB |
Output is correct |
35 |
Correct |
299 ms |
94852 KB |
Output is correct |
36 |
Correct |
186 ms |
37100 KB |
Output is correct |
37 |
Correct |
139 ms |
13636 KB |
Output is correct |
38 |
Correct |
137 ms |
12356 KB |
Output is correct |
39 |
Correct |
138 ms |
12228 KB |
Output is correct |
40 |
Correct |
139 ms |
12100 KB |
Output is correct |
41 |
Correct |
201 ms |
12100 KB |
Output is correct |
42 |
Correct |
151 ms |
12100 KB |
Output is correct |
43 |
Correct |
191 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 |
Runtime error |
6 ms |
492 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |