#include "books.h"
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define ii pair<int, int>
#define x first
#define y second
#define vii vector<ii>
#define pb push_back
#define all(x) x.begin(), x.end()
#define loop(i,s,e) for(int i=s;i<e;++i)
#define chkmax(a,b) a = max(a,b)
#define chkmin(a,b) a = min(a,b)
using namespace std;
const int INF = 1e9;
typedef long long ll;
ll minimum_walk(vi p, int s) {
ll ans = 0;
int n = p.size();
vb check(n,0);
int needl = s, needr = s;
vi v1(n), v2(n);
loop(i,0,n) if(!check[i]){
vi vec;
int mn = i, mx = i;
for(int cur = i;!check[cur]; cur = p[cur]){
check[cur] = 1;
ans+=abs(cur-p[cur]);
vec.pb(cur);
chkmin(mn, cur); chkmax(mx, cur);
}
if (vec.size()>1) chkmax(needr, mx), chkmin(needl, mn);
for(auto x:vec) v2[x] = mx, v1[x] = mn;
//cout<<"HI: "<<i<<endl;
}
if (s==0){
int cur = 0, r=0;
while(cur<=needr){
if (cur>r) ans+=2;
chkmax(r, v2[cur++]);
}
return ans;
}
vvi maxi(n, vi(n)), mini(n, vi(n)), dp(n, vi(n,INF));
loop(i,0,n) maxi[i][i] = v2[i], mini[i][i] = v1[i];
loop(k,1,n){
loop(i,0,n-k){
int j = i+k;
maxi[i][j] = max(maxi[i][j-1], maxi[i+1][j]);
mini[i][j] = min(mini[i][j-1], mini[i+1][j]);
}
}
dp[s][s] = 0;
loop(k,0,n){
loop(i,0,n-k){
int j = i+k;
if (i) chkmin(dp[i-1][j], dp[i][j]+1);
if (j<n) chkmin(dp[i][j+1], dp[i][j]+1);
chkmin(dp[mini[i][j]][maxi[i][j]], dp[i][j]);
}
}
int best = INF;
loop(i,0,needl+1) loop(j,needr,n) chkmin(best, dp[i][j]);
ans+=2*best;
return ans;
}
/*
clear
g++ books.cpp grader.cpp -o a ; ./a
4 0
0 2 3 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
372 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
372 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
372 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
416 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
372 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
372 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
416 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
256 ms |
27112 KB |
Output is correct |
31 |
Correct |
253 ms |
25068 KB |
Output is correct |
32 |
Correct |
200 ms |
22776 KB |
Output is correct |
33 |
Correct |
224 ms |
23032 KB |
Output is correct |
34 |
Correct |
220 ms |
23160 KB |
Output is correct |
35 |
Correct |
219 ms |
22904 KB |
Output is correct |
36 |
Correct |
217 ms |
22904 KB |
Output is correct |
37 |
Correct |
187 ms |
22904 KB |
Output is correct |
38 |
Correct |
191 ms |
23032 KB |
Output is correct |
39 |
Correct |
187 ms |
23032 KB |
Output is correct |
40 |
Correct |
196 ms |
23220 KB |
Output is correct |
41 |
Correct |
243 ms |
26032 KB |
Output is correct |
42 |
Correct |
208 ms |
24676 KB |
Output is correct |
43 |
Correct |
239 ms |
22904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
12160 KB |
Output is correct |
2 |
Correct |
24 ms |
12160 KB |
Output is correct |
3 |
Correct |
32 ms |
12288 KB |
Output is correct |
4 |
Correct |
25 ms |
12160 KB |
Output is correct |
5 |
Correct |
30 ms |
12284 KB |
Output is correct |
6 |
Correct |
26 ms |
12288 KB |
Output is correct |
7 |
Correct |
24 ms |
12160 KB |
Output is correct |
8 |
Correct |
24 ms |
12160 KB |
Output is correct |
9 |
Correct |
25 ms |
12160 KB |
Output is correct |
10 |
Correct |
30 ms |
12160 KB |
Output is correct |
11 |
Correct |
27 ms |
12160 KB |
Output is correct |
12 |
Correct |
27 ms |
12160 KB |
Output is correct |
13 |
Correct |
26 ms |
12160 KB |
Output is correct |
14 |
Correct |
26 ms |
12160 KB |
Output is correct |
15 |
Correct |
27 ms |
12160 KB |
Output is correct |
16 |
Correct |
23 ms |
12160 KB |
Output is correct |
17 |
Correct |
26 ms |
12160 KB |
Output is correct |
18 |
Correct |
27 ms |
12160 KB |
Output is correct |
19 |
Correct |
27 ms |
12288 KB |
Output is correct |
20 |
Correct |
25 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
372 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
372 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
416 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
256 ms |
27112 KB |
Output is correct |
31 |
Correct |
253 ms |
25068 KB |
Output is correct |
32 |
Correct |
200 ms |
22776 KB |
Output is correct |
33 |
Correct |
224 ms |
23032 KB |
Output is correct |
34 |
Correct |
220 ms |
23160 KB |
Output is correct |
35 |
Correct |
219 ms |
22904 KB |
Output is correct |
36 |
Correct |
217 ms |
22904 KB |
Output is correct |
37 |
Correct |
187 ms |
22904 KB |
Output is correct |
38 |
Correct |
191 ms |
23032 KB |
Output is correct |
39 |
Correct |
187 ms |
23032 KB |
Output is correct |
40 |
Correct |
196 ms |
23220 KB |
Output is correct |
41 |
Correct |
243 ms |
26032 KB |
Output is correct |
42 |
Correct |
208 ms |
24676 KB |
Output is correct |
43 |
Correct |
239 ms |
22904 KB |
Output is correct |
44 |
Correct |
28 ms |
12160 KB |
Output is correct |
45 |
Correct |
24 ms |
12160 KB |
Output is correct |
46 |
Correct |
32 ms |
12288 KB |
Output is correct |
47 |
Correct |
25 ms |
12160 KB |
Output is correct |
48 |
Correct |
30 ms |
12284 KB |
Output is correct |
49 |
Correct |
26 ms |
12288 KB |
Output is correct |
50 |
Correct |
24 ms |
12160 KB |
Output is correct |
51 |
Correct |
24 ms |
12160 KB |
Output is correct |
52 |
Correct |
25 ms |
12160 KB |
Output is correct |
53 |
Correct |
30 ms |
12160 KB |
Output is correct |
54 |
Correct |
27 ms |
12160 KB |
Output is correct |
55 |
Correct |
27 ms |
12160 KB |
Output is correct |
56 |
Correct |
26 ms |
12160 KB |
Output is correct |
57 |
Correct |
26 ms |
12160 KB |
Output is correct |
58 |
Correct |
27 ms |
12160 KB |
Output is correct |
59 |
Correct |
23 ms |
12160 KB |
Output is correct |
60 |
Correct |
26 ms |
12160 KB |
Output is correct |
61 |
Correct |
27 ms |
12160 KB |
Output is correct |
62 |
Correct |
27 ms |
12288 KB |
Output is correct |
63 |
Correct |
25 ms |
12160 KB |
Output is correct |
64 |
Runtime error |
1004 ms |
1048580 KB |
Execution killed with signal 9 |
65 |
Halted |
0 ms |
0 KB |
- |