#include <bits/stdc++.h>
#include "books.h"
using std::vector;
using std::array;
using std::pair;
using std::tuple;
using ll = long long;
ll minimum_walk(vector<int> P, int src) {
{
int l = 0;
while (l < src and P[l] == l) {
l += 1;
}
int r = (int)P.size() - 1;
while (r > src and P[r] == r) {
r -= 1;
}
P.erase(P.begin() + r + 1, P.end());
P.erase(P.begin(), P.begin() + l);
for (auto& x : P) {
x -= l;
}
src -= l;
}
const int N = P.size();
assert(N <= 1000);
vector<int> min(N), max(N);
vector<char> done(N);
ll ret = 0;
for (int i = 0; i < N; ++i) {
if (!done[i]) {
vector<int> list;
for (int j = i; !done[j]; j = P[j]) {
list.push_back(j);
ret += std::abs(P[j] - j);
done[j] = true;
}
const int l = *std::min_element(list.begin(), list.end());
const int r = *std::max_element(list.begin(), list.end());
for (const int j : list) {
min[j] = l;
max[j] = r;
}
}
}
vector dist(N, vector(N, N + 1));
std::deque<pair<int, int>> que;
const auto push0 = [&](const int l, const int r, const int d) {
if (dist[l][r] > d) {
dist[l][r] = d;
que.emplace_front(l, r);
}
};
const auto push1 = [&](const int l, const int r, const int d) {
if (dist[l][r] > d + 1) {
dist[l][r] = d + 1;
que.emplace_back(l, r);
}
};
push0(src, src, 0);
while (!que.empty()) {
const auto [l, r] = que.front();
que.pop_front();
push0(std::min({l, min[l], min[r]}), std::max({r, max[l], max[r]}), dist[l][r]);
if (l < r) {
push0(l + 1, r, dist[l][r]);
push0(l, r - 1, dist[l][r]);
}
if (l > 0) {
push1(l - 1, r, dist[l][r]);
}
if (r + 1 < N) {
push1(l, r + 1, dist[l][r]);
}
}
return ret + 2 * dist[0][N - 1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
21 ms |
8140 KB |
Output is correct |
20 |
Correct |
22 ms |
8204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
19 ms |
4284 KB |
Output is correct |
23 |
Correct |
17 ms |
4300 KB |
Output is correct |
24 |
Correct |
15 ms |
4300 KB |
Output is correct |
25 |
Correct |
16 ms |
4292 KB |
Output is correct |
26 |
Correct |
17 ms |
4504 KB |
Output is correct |
27 |
Correct |
17 ms |
5180 KB |
Output is correct |
28 |
Correct |
19 ms |
5324 KB |
Output is correct |
29 |
Correct |
19 ms |
7088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
21 ms |
8140 KB |
Output is correct |
20 |
Correct |
22 ms |
8204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
19 ms |
4284 KB |
Output is correct |
23 |
Correct |
17 ms |
4300 KB |
Output is correct |
24 |
Correct |
15 ms |
4300 KB |
Output is correct |
25 |
Correct |
16 ms |
4292 KB |
Output is correct |
26 |
Correct |
17 ms |
4504 KB |
Output is correct |
27 |
Correct |
17 ms |
5180 KB |
Output is correct |
28 |
Correct |
19 ms |
5324 KB |
Output is correct |
29 |
Correct |
19 ms |
7088 KB |
Output is correct |
30 |
Runtime error |
91 ms |
16356 KB |
Execution killed with signal 6 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
5816 KB |
Output is correct |
2 |
Correct |
23 ms |
7300 KB |
Output is correct |
3 |
Correct |
19 ms |
6084 KB |
Output is correct |
4 |
Correct |
13 ms |
4236 KB |
Output is correct |
5 |
Correct |
19 ms |
4384 KB |
Output is correct |
6 |
Correct |
20 ms |
7116 KB |
Output is correct |
7 |
Correct |
20 ms |
6984 KB |
Output is correct |
8 |
Correct |
18 ms |
5348 KB |
Output is correct |
9 |
Correct |
18 ms |
5524 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
12 ms |
4380 KB |
Output is correct |
12 |
Correct |
12 ms |
4432 KB |
Output is correct |
13 |
Correct |
16 ms |
4420 KB |
Output is correct |
14 |
Correct |
17 ms |
4472 KB |
Output is correct |
15 |
Correct |
21 ms |
5196 KB |
Output is correct |
16 |
Correct |
23 ms |
5956 KB |
Output is correct |
17 |
Correct |
13 ms |
4264 KB |
Output is correct |
18 |
Correct |
13 ms |
4304 KB |
Output is correct |
19 |
Correct |
17 ms |
4304 KB |
Output is correct |
20 |
Correct |
20 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
21 ms |
8140 KB |
Output is correct |
20 |
Correct |
22 ms |
8204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
19 ms |
4284 KB |
Output is correct |
23 |
Correct |
17 ms |
4300 KB |
Output is correct |
24 |
Correct |
15 ms |
4300 KB |
Output is correct |
25 |
Correct |
16 ms |
4292 KB |
Output is correct |
26 |
Correct |
17 ms |
4504 KB |
Output is correct |
27 |
Correct |
17 ms |
5180 KB |
Output is correct |
28 |
Correct |
19 ms |
5324 KB |
Output is correct |
29 |
Correct |
19 ms |
7088 KB |
Output is correct |
30 |
Runtime error |
91 ms |
16356 KB |
Execution killed with signal 6 |
31 |
Halted |
0 ms |
0 KB |
- |