#include <bits/stdc++.h>
#include "books.h"
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 1010;
const ll OO = 1e18;
vector<array<int, 2> > vc;
queue<int> q;
vector<ll> f[N];
ll ans = 0, mn = ll(1e9);
int n, lf, rt, le[N], ri[N];
long long minimum_walk(std::vector<int> p, int s) {
n = sz(p);
if (n > 1000){
vc.clear();
for (int i = 0; i < n; i++){
ans += abs(p[i] - i);
if (p[i] != i)
vc.PB({min(p[i], i), max(p[i], i)});
}
if (sz(vc)) {
sort(all(vc));
int lst = 0;
for (array<int, 2> cr : vc)
if (cr[0] > lst){
ans += 2 * (cr[0] - lst);
lst = cr[1];
} else lst = max(lst, cr[1]);
}
return ans;
}
bool was = 0;
for (int i = 0; i < n; i++){
ans += abs(p[i] - i);
if (p[i] != i) {
was = 1;
lf = min(lf, i);
rt = max(rt, i);
}
}
if (!was) return 0;
for (int i = 0; i < n; i++){
le[i] = ri[i] = i;
q.push(i);
while (sz(q)){
int v = q.front(); q.pop();
int nl = v, nr = p[v];
if (nl > nr) swap(nl, nr);
if (nl < le[i]){
for (int j = nl; j < le[i]; j++)
q.push(j);
le[i] = nl;
}
if (nr > ri[i]){
for (int j = ri[i] + 1; j <= nr; j++)
q.push(j);
ri[i] = nr;
}
}
}
for (int i = lf; i <= s; i++){
f[i].resize(n);
fill(all(f[i]), OO);
}
f[le[s]][ri[s]] = 0;
int len = rt - lf + 1;
for (int k = 1; k < len; k++)
for (int l = lf; l <= s && l + k - 1 <= rt; l++){
int r = l + k - 1;
if (f[l][r] == OO) continue;
if (l > lf){
int nl = min(l - 1, le[l - 1]);
int nr = max(r, ri[l - 1]);
f[nl][nr] = min(f[nl][nr], f[l][r] + 2);
}
if (r < rt){
int nl = min(l, le[r + 1]);
int nr = max(r + 1, ri[r + 1]);
f[nl][nr] = min(f[nl][nr], f[l][r] + 2);
}
}
return ans + f[lf][rt];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
14 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
14 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
316 ms |
16580 KB |
Output is correct |
31 |
Correct |
313 ms |
17252 KB |
Output is correct |
32 |
Correct |
156 ms |
8824 KB |
Output is correct |
33 |
Correct |
187 ms |
13032 KB |
Output is correct |
34 |
Correct |
189 ms |
13004 KB |
Output is correct |
35 |
Correct |
210 ms |
17252 KB |
Output is correct |
36 |
Correct |
222 ms |
17124 KB |
Output is correct |
37 |
Correct |
248 ms |
17124 KB |
Output is correct |
38 |
Correct |
257 ms |
17380 KB |
Output is correct |
39 |
Correct |
258 ms |
17256 KB |
Output is correct |
40 |
Correct |
275 ms |
17124 KB |
Output is correct |
41 |
Correct |
293 ms |
17124 KB |
Output is correct |
42 |
Correct |
292 ms |
17252 KB |
Output is correct |
43 |
Correct |
241 ms |
13032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
3712 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
9 ms |
3456 KB |
Output is correct |
4 |
Correct |
11 ms |
5120 KB |
Output is correct |
5 |
Correct |
11 ms |
4224 KB |
Output is correct |
6 |
Correct |
8 ms |
3456 KB |
Output is correct |
7 |
Correct |
9 ms |
4224 KB |
Output is correct |
8 |
Correct |
9 ms |
4224 KB |
Output is correct |
9 |
Correct |
10 ms |
5888 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
12 ms |
3968 KB |
Output is correct |
12 |
Correct |
12 ms |
4480 KB |
Output is correct |
13 |
Correct |
9 ms |
5248 KB |
Output is correct |
14 |
Correct |
8 ms |
3840 KB |
Output is correct |
15 |
Correct |
10 ms |
4864 KB |
Output is correct |
16 |
Correct |
8 ms |
3584 KB |
Output is correct |
17 |
Correct |
10 ms |
4096 KB |
Output is correct |
18 |
Correct |
9 ms |
4480 KB |
Output is correct |
19 |
Correct |
9 ms |
4096 KB |
Output is correct |
20 |
Correct |
12 ms |
6528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
14 ms |
384 KB |
Output is correct |
20 |
Correct |
14 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
316 ms |
16580 KB |
Output is correct |
31 |
Correct |
313 ms |
17252 KB |
Output is correct |
32 |
Correct |
156 ms |
8824 KB |
Output is correct |
33 |
Correct |
187 ms |
13032 KB |
Output is correct |
34 |
Correct |
189 ms |
13004 KB |
Output is correct |
35 |
Correct |
210 ms |
17252 KB |
Output is correct |
36 |
Correct |
222 ms |
17124 KB |
Output is correct |
37 |
Correct |
248 ms |
17124 KB |
Output is correct |
38 |
Correct |
257 ms |
17380 KB |
Output is correct |
39 |
Correct |
258 ms |
17256 KB |
Output is correct |
40 |
Correct |
275 ms |
17124 KB |
Output is correct |
41 |
Correct |
293 ms |
17124 KB |
Output is correct |
42 |
Correct |
292 ms |
17252 KB |
Output is correct |
43 |
Correct |
241 ms |
13032 KB |
Output is correct |
44 |
Correct |
11 ms |
3712 KB |
Output is correct |
45 |
Correct |
5 ms |
640 KB |
Output is correct |
46 |
Correct |
9 ms |
3456 KB |
Output is correct |
47 |
Correct |
11 ms |
5120 KB |
Output is correct |
48 |
Correct |
11 ms |
4224 KB |
Output is correct |
49 |
Correct |
8 ms |
3456 KB |
Output is correct |
50 |
Correct |
9 ms |
4224 KB |
Output is correct |
51 |
Correct |
9 ms |
4224 KB |
Output is correct |
52 |
Correct |
10 ms |
5888 KB |
Output is correct |
53 |
Correct |
5 ms |
384 KB |
Output is correct |
54 |
Correct |
12 ms |
3968 KB |
Output is correct |
55 |
Correct |
12 ms |
4480 KB |
Output is correct |
56 |
Correct |
9 ms |
5248 KB |
Output is correct |
57 |
Correct |
8 ms |
3840 KB |
Output is correct |
58 |
Correct |
10 ms |
4864 KB |
Output is correct |
59 |
Correct |
8 ms |
3584 KB |
Output is correct |
60 |
Correct |
10 ms |
4096 KB |
Output is correct |
61 |
Correct |
9 ms |
4480 KB |
Output is correct |
62 |
Correct |
9 ms |
4096 KB |
Output is correct |
63 |
Correct |
12 ms |
6528 KB |
Output is correct |
64 |
Incorrect |
233 ms |
19152 KB |
3rd lines differ - on the 1st token, expected: '9339550', found: '9338938' |
65 |
Halted |
0 ms |
0 KB |
- |