#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);
vvi maxi(n, vi(n)), mini(n, vi(n)), dp(n, vi(n,INF));
int needl = s, needr = s;
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) maxi[x][x] = mx, mini[x][x] = mn;
//cout<<"HI: "<<i<<endl;
}
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 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 |
1 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 |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 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 |
1 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 |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
24 ms |
12160 KB |
Output is correct |
20 |
Correct |
27 ms |
12160 KB |
Output is correct |
21 |
Correct |
24 ms |
12160 KB |
Output is correct |
22 |
Correct |
23 ms |
12160 KB |
Output is correct |
23 |
Correct |
27 ms |
12160 KB |
Output is correct |
24 |
Correct |
26 ms |
12160 KB |
Output is correct |
25 |
Correct |
24 ms |
12160 KB |
Output is correct |
26 |
Correct |
26 ms |
12288 KB |
Output is correct |
27 |
Correct |
24 ms |
12160 KB |
Output is correct |
28 |
Correct |
26 ms |
12160 KB |
Output is correct |
29 |
Correct |
25 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 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 |
1 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 |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
24 ms |
12160 KB |
Output is correct |
20 |
Correct |
27 ms |
12160 KB |
Output is correct |
21 |
Correct |
24 ms |
12160 KB |
Output is correct |
22 |
Correct |
23 ms |
12160 KB |
Output is correct |
23 |
Correct |
27 ms |
12160 KB |
Output is correct |
24 |
Correct |
26 ms |
12160 KB |
Output is correct |
25 |
Correct |
24 ms |
12160 KB |
Output is correct |
26 |
Correct |
26 ms |
12288 KB |
Output is correct |
27 |
Correct |
24 ms |
12160 KB |
Output is correct |
28 |
Correct |
26 ms |
12160 KB |
Output is correct |
29 |
Correct |
25 ms |
12288 KB |
Output is correct |
30 |
Runtime error |
986 ms |
1048580 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
12160 KB |
Output is correct |
2 |
Correct |
29 ms |
12160 KB |
Output is correct |
3 |
Correct |
32 ms |
12160 KB |
Output is correct |
4 |
Correct |
29 ms |
12160 KB |
Output is correct |
5 |
Correct |
30 ms |
12160 KB |
Output is correct |
6 |
Correct |
28 ms |
12160 KB |
Output is correct |
7 |
Correct |
28 ms |
12160 KB |
Output is correct |
8 |
Correct |
32 ms |
12160 KB |
Output is correct |
9 |
Correct |
35 ms |
12160 KB |
Output is correct |
10 |
Correct |
30 ms |
12288 KB |
Output is correct |
11 |
Correct |
36 ms |
12160 KB |
Output is correct |
12 |
Correct |
35 ms |
12160 KB |
Output is correct |
13 |
Correct |
31 ms |
12160 KB |
Output is correct |
14 |
Correct |
29 ms |
12160 KB |
Output is correct |
15 |
Correct |
32 ms |
12288 KB |
Output is correct |
16 |
Correct |
28 ms |
12160 KB |
Output is correct |
17 |
Correct |
29 ms |
12160 KB |
Output is correct |
18 |
Correct |
31 ms |
12160 KB |
Output is correct |
19 |
Correct |
30 ms |
12160 KB |
Output is correct |
20 |
Correct |
29 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 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 |
1 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 |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
24 ms |
12160 KB |
Output is correct |
20 |
Correct |
27 ms |
12160 KB |
Output is correct |
21 |
Correct |
24 ms |
12160 KB |
Output is correct |
22 |
Correct |
23 ms |
12160 KB |
Output is correct |
23 |
Correct |
27 ms |
12160 KB |
Output is correct |
24 |
Correct |
26 ms |
12160 KB |
Output is correct |
25 |
Correct |
24 ms |
12160 KB |
Output is correct |
26 |
Correct |
26 ms |
12288 KB |
Output is correct |
27 |
Correct |
24 ms |
12160 KB |
Output is correct |
28 |
Correct |
26 ms |
12160 KB |
Output is correct |
29 |
Correct |
25 ms |
12288 KB |
Output is correct |
30 |
Runtime error |
986 ms |
1048580 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |