#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int N = 1e3 + 10;
int mk[N];
int L[N], R[N];
int mn[N][N], mx[N][N];
ll dp[N][N];
ll minimum_walk(vector<int> p, int s) {
int n = p.size();
ll ans = 0;
ll near = n;
for(int i = 0; i < n; i++){
if(p[i] != i){
ans += abs(p[i] - i);
near = min(near, (ll) abs(s - i));
} else {
mk[i] = 1;
R[i] = i;
L[i] = i;
}
}
int com = 0;
vector<pii> seg;
for(int i = 0; i < n; i++){
if(mk[i]) continue;
com ++;
vector<int> vis;
int nw = i;
while(!mk[nw]){
mk[nw] = 1;
vis.pb(nw);
nw = p[nw];
}
sort(all(vis));
seg.pb({vis[0], vis.back()});
for(auto x : vis) L[x] = vis[0];
for(auto x : vis) R[x] = vis.back();
}
if(com == 0) return 0;
for(int i = 0; i < n; i++) mn[i][i] = L[i];
for(int i = 0; i < n; i++) mx[i][i] = R[i];
for(int ln = 1; ln < n; ln ++){
for(int i = 0; i + ln < n; i++){
int j = i + ln;
mn[i][j] = min(mn[i][j - 1], mn[i + 1][j]);
mx[i][j] = max(mx[i][j - 1], mx[i + 1][j]);
}
}
if(ans == 0) return 0;
int st = 0, en = n - 1;
while(p[st] == st) st ++;
while(p[en] == en) en --;
ll Inf = 1e18;
for(int ln = n - 1; ln >= 0; ln--){
for(int i = 0; i + ln < n; i++){
int j = i + ln;
if(i <= st && en <= j) dp[i][j] = 0;
else if(mn[i][j] != i || mx[i][j] != j){
dp[i][j] = dp[mn[i][j]][mx[i][j]];
} else {
dp[i][j] = 2 + min(i ? dp[i - 1][j] : Inf, j + 1 < n ? dp[i][j + 1] : Inf);
}
}
}
return ans + dp[s][s];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
31 ms |
15216 KB |
Output is correct |
20 |
Correct |
31 ms |
15224 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
32 ms |
15224 KB |
Output is correct |
23 |
Correct |
30 ms |
15148 KB |
Output is correct |
24 |
Correct |
39 ms |
15232 KB |
Output is correct |
25 |
Correct |
48 ms |
15200 KB |
Output is correct |
26 |
Correct |
36 ms |
15224 KB |
Output is correct |
27 |
Correct |
31 ms |
15224 KB |
Output is correct |
28 |
Correct |
30 ms |
15232 KB |
Output is correct |
29 |
Correct |
31 ms |
15232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
31 ms |
15216 KB |
Output is correct |
20 |
Correct |
31 ms |
15224 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
32 ms |
15224 KB |
Output is correct |
23 |
Correct |
30 ms |
15148 KB |
Output is correct |
24 |
Correct |
39 ms |
15232 KB |
Output is correct |
25 |
Correct |
48 ms |
15200 KB |
Output is correct |
26 |
Correct |
36 ms |
15224 KB |
Output is correct |
27 |
Correct |
31 ms |
15224 KB |
Output is correct |
28 |
Correct |
30 ms |
15232 KB |
Output is correct |
29 |
Correct |
31 ms |
15232 KB |
Output is correct |
30 |
Runtime error |
178 ms |
18240 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15224 KB |
Output is correct |
2 |
Correct |
31 ms |
15224 KB |
Output is correct |
3 |
Correct |
29 ms |
15224 KB |
Output is correct |
4 |
Correct |
29 ms |
15232 KB |
Output is correct |
5 |
Correct |
25 ms |
15232 KB |
Output is correct |
6 |
Correct |
28 ms |
15224 KB |
Output is correct |
7 |
Correct |
25 ms |
15232 KB |
Output is correct |
8 |
Correct |
26 ms |
15232 KB |
Output is correct |
9 |
Correct |
24 ms |
15232 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
26 ms |
15232 KB |
Output is correct |
12 |
Correct |
28 ms |
15232 KB |
Output is correct |
13 |
Correct |
24 ms |
15232 KB |
Output is correct |
14 |
Correct |
20 ms |
15232 KB |
Output is correct |
15 |
Correct |
26 ms |
15224 KB |
Output is correct |
16 |
Correct |
23 ms |
15232 KB |
Output is correct |
17 |
Correct |
26 ms |
15224 KB |
Output is correct |
18 |
Correct |
26 ms |
15232 KB |
Output is correct |
19 |
Correct |
25 ms |
15232 KB |
Output is correct |
20 |
Correct |
25 ms |
15224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
31 ms |
15216 KB |
Output is correct |
20 |
Correct |
31 ms |
15224 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
32 ms |
15224 KB |
Output is correct |
23 |
Correct |
30 ms |
15148 KB |
Output is correct |
24 |
Correct |
39 ms |
15232 KB |
Output is correct |
25 |
Correct |
48 ms |
15200 KB |
Output is correct |
26 |
Correct |
36 ms |
15224 KB |
Output is correct |
27 |
Correct |
31 ms |
15224 KB |
Output is correct |
28 |
Correct |
30 ms |
15232 KB |
Output is correct |
29 |
Correct |
31 ms |
15232 KB |
Output is correct |
30 |
Runtime error |
178 ms |
18240 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |