// #include "books.h"
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'
const int INF = 1e9;
long long minimum_walk(std::vector<int> p, int s) {
int n = sz(p);
ll ans = 0;
for (int i = 0; i < n; i++){
ans += abs(i - p[i]);
}
int mincycle = -1, maxcycle = -1;
for (int i = 0; i < n; i++) if (i != p[i]){
mincycle = i;
break;
}
for (int i = n - 1; i >= 0; i--) if (i != p[i]){
maxcycle = i;
break;
}
if (mincycle == -1) return 0;
for (int i = mincycle, mx = -1; i < n;){
mx = max(mx, p[i]);
if (mx <= i){
int j = i + 1;
while (j < n && j == p[j]) j++;
if (j == n) break;
ans += 2 * (j - i), i = j;
}
else i++;
}
if (s < mincycle) return ans + 2 * (mincycle - s);
if (s > maxcycle) return ans + 2 * (s - maxcycle);
vector<bool> cycleend(n);
for (int i = 0, mx = -1; i < n; i++){
mx = max(mx, p[i]);
if (mx <= i) cycleend[i] = 1;
}
vector<int> cur, used(n), cid(n), minpos(n, INF), maxpos(n, -1);
vector<int> minl(n, INF), maxr(n, -1);
int id = 0;
function<void(int)> dfs = [&] (int v){
used[v] = 1, cid[v] = id;
minpos[id] = min(minpos[id], v);
maxpos[id] = max(maxpos[id], v);
if (!used[p[v]]) dfs(p[v]);
};
for (int i = 0; i < n; i++){
if (used[i]) continue;
dfs(i);
id++;
}
vector<vector<int>> g(id);
for (int i = 0; i < id; i++){
for (int j = minpos[i]; j <= maxpos[i]; j++){
g[cid[j]].pb(i);
}
}
function<void(int, int)> dfs1 = [&] (int v, int val){
minl[v] = val;
for (auto u : g[v]) if (minl[u] == INF) dfs1(u, val);
};
function<void(int, int)> dfs2 = [&] (int v, int val){
maxr[v] = val;
for (auto u : g[v]){
if (maxr[u] == -1) dfs2(u, val);
}
};
vector<int> order(id);
iota(all(order), 0);
sort(all(order), [&] (int i, int j){
return minpos[i] < minpos[j];
});
for (auto i : order){
if (minl[i] == INF) dfs1(i, minpos[i]);
}
sort(all(order), [&] (int i, int j){
return maxpos[i] > maxpos[j];
});
for (auto i : order){
if (maxr[i] == -1) dfs2(i, maxpos[i]);
}
vector<vector<int>> dp(n, vector<int>(n, INF));
dp[minl[cid[s]]][maxr[cid[s]]] = 0;
ll ans1;
for (int len = 1; len <= n; len++){
for (int l = 0; l + len - 1 < n; l++){
int r = l + len - 1;
if (dp[l][r] == INF) continue;
ans1 = dp[l][r];
if (l > 0 && !cycleend[l - 1]){
int newl = min(l, minl[cid[l - 1]]), newr = max(r, maxr[cid[l - 1]]);
dp[newl][newr] = min(dp[newl][newr], dp[l][r] + 2);
}
if (!cycleend[r] && r < n - 1){
int newl = min(l, minl[cid[r + 1]]), newr = max(r, maxr[cid[r + 1]]);
dp[newl][newr] = min(dp[newl][newr], dp[l][r] + 2);
}
}
}
return ans + ans1;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:129:16: warning: 'ans1' may be used uninitialized in this function [-Wmaybe-uninitialized]
129 | return ans + ans1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
3 ms |
4264 KB |
Output is correct |
20 |
Correct |
3 ms |
4308 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
4308 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
3 ms |
4264 KB |
Output is correct |
20 |
Correct |
3 ms |
4308 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
4308 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
3 ms |
4308 KB |
Output is correct |
30 |
Runtime error |
770 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
4 ms |
4308 KB |
Output is correct |
4 |
Correct |
3 ms |
4268 KB |
Output is correct |
5 |
Correct |
3 ms |
4264 KB |
Output is correct |
6 |
Correct |
3 ms |
4308 KB |
Output is correct |
7 |
Correct |
3 ms |
4308 KB |
Output is correct |
8 |
Correct |
3 ms |
4308 KB |
Output is correct |
9 |
Correct |
3 ms |
4308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
5588 KB |
Output is correct |
12 |
Correct |
4 ms |
5588 KB |
Output is correct |
13 |
Correct |
3 ms |
4308 KB |
Output is correct |
14 |
Correct |
3 ms |
4308 KB |
Output is correct |
15 |
Correct |
3 ms |
4308 KB |
Output is correct |
16 |
Correct |
3 ms |
4308 KB |
Output is correct |
17 |
Correct |
4 ms |
4692 KB |
Output is correct |
18 |
Correct |
3 ms |
4564 KB |
Output is correct |
19 |
Correct |
5 ms |
4436 KB |
Output is correct |
20 |
Correct |
3 ms |
4308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
3 ms |
4264 KB |
Output is correct |
20 |
Correct |
3 ms |
4308 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
4308 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
3 ms |
4308 KB |
Output is correct |
30 |
Runtime error |
770 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |