#include "books.h"
#include <bits/stdc++.h>
using ll = long long;
using std::vector;
ll minimum_walk(vector<int> P, int s) {
{
int l = 0, r = (int)P.size() - 1;
while (l < s and P[l] == l) {
l += 1;
}
while (r > s and P[r] == r) {
r -= 1;
}
P.erase(P.begin() + r + 1, P.end());
P.erase(P.begin(), P.begin() + l);
s -= l;
}
const int N = (int)P.size();
vector<char> seen(N);
vector<int> left(N), right(N);
ll ans = 0;
for (int i = 0; i < N; ++i) {
ans += std::abs(P[i] - i);
if (seen[i]) {
continue;
}
vector<int> cycle;
for (int j = i; !seen[j]; j = P[j]) {
seen[j] = true;
cycle.push_back(j);
}
const int l = *std::min_element(cycle.begin(), cycle.end());
const int r = *std::max_element(cycle.begin(), cycle.end());
for (const int j : cycle) {
left[j] = l;
right[j] = r;
}
}
int a = left[s], b = right[s], l = s, r = s;
const auto add = [&](const int k) {
a = std::min(a, left[k]);
b = std::max(b, right[k]);
};
while (true) {
while (a < l or r < b) {
if (a < l) {
l -= 1;
add(l);
} else {
r += 1;
add(r);
}
}
if (0 == l and r == N - 1) {
break;
}
int x = l, xcost = 0, xr = r;
while (0 < x and xr <= r) {
x -= 1;
xcost += 1;
int k = left[x];
xr = std::max(xr, right[x]);
while (k < x) {
x -= 1;
k = std::min(k, left[x]);
xr = std::max(xr, right[x]);
}
}
int y = r, ycost = 0, yl = l;
while (y < N - 1 and yl >= l) {
y += 1;
ycost += 1;
int k = right[y];
yl = std::min(yl, left[y]);
while (y < k) {
y += 1;
k = std::max(k, right[y]);
yl = std::min(yl, left[y]);
}
}
if (right[x] <= r) {
ans += 2 * (xcost + ycost);
break;
}
if (xcost < ycost) {
ans += 2 * xcost;
add(xr);
} else {
ans += 2 * ycost;
add(yl);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
138 ms |
21092 KB |
Output is correct |
31 |
Correct |
134 ms |
19076 KB |
Output is correct |
32 |
Correct |
78 ms |
8012 KB |
Output is correct |
33 |
Correct |
117 ms |
16932 KB |
Output is correct |
34 |
Correct |
117 ms |
16872 KB |
Output is correct |
35 |
Correct |
124 ms |
16876 KB |
Output is correct |
36 |
Correct |
113 ms |
17060 KB |
Output is correct |
37 |
Correct |
99 ms |
16888 KB |
Output is correct |
38 |
Correct |
99 ms |
16876 KB |
Output is correct |
39 |
Correct |
99 ms |
16876 KB |
Output is correct |
40 |
Correct |
102 ms |
17320 KB |
Output is correct |
41 |
Correct |
118 ms |
19920 KB |
Output is correct |
42 |
Correct |
113 ms |
18580 KB |
Output is correct |
43 |
Correct |
116 ms |
16844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '5920', found: '6428' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
138 ms |
21092 KB |
Output is correct |
31 |
Correct |
134 ms |
19076 KB |
Output is correct |
32 |
Correct |
78 ms |
8012 KB |
Output is correct |
33 |
Correct |
117 ms |
16932 KB |
Output is correct |
34 |
Correct |
117 ms |
16872 KB |
Output is correct |
35 |
Correct |
124 ms |
16876 KB |
Output is correct |
36 |
Correct |
113 ms |
17060 KB |
Output is correct |
37 |
Correct |
99 ms |
16888 KB |
Output is correct |
38 |
Correct |
99 ms |
16876 KB |
Output is correct |
39 |
Correct |
99 ms |
16876 KB |
Output is correct |
40 |
Correct |
102 ms |
17320 KB |
Output is correct |
41 |
Correct |
118 ms |
19920 KB |
Output is correct |
42 |
Correct |
113 ms |
18580 KB |
Output is correct |
43 |
Correct |
116 ms |
16844 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '5920', found: '6428' |
47 |
Halted |
0 ms |
0 KB |
- |