#include "books.h"
#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 1ll << 50;
void chmax(i64 &a, i64 b) {
if (a < b) a = b;
}
long long minimum_walk(std::vector<int> p, int s) {
const int n = (int)p.size();
std::vector<i64> imos(n), imos2(n);
std::vector<int> revP(n);
for (int i = 0; i < n; ++i) {
revP[p[i]] = i;
int a = i, b = p[i];
if (a > b) std::swap(a, b);
++imos[a];
--imos[b];
if (a != b) {
++imos2[a + 1];
--imos2[b];
}
}
for (int i = 0; i < n - 1; ++i) {
imos[i + 1] += imos[i];
imos2[i + 1] += imos2[i];
}
i64 baseCost = std::accumulate(imos.begin(), imos.end(), 0ll);
auto solveR = [&](const int f) {
i64 x = 0, c = 0;
for (int i = f; i < n; ++i) {
if (imos[i] == 0) {
++c;
}
if (imos[i] != 0) {
x += c;
c = 0;
}
}
return x;
};
auto solveL = [&](const int f) {
i64 x = 0, c = 0;
for (int i = f - 1; i >= 0; --i) {
if (imos[i] == 0) {
++c;
}
if (imos[i] != 0) {
x += c;
c = 0;
}
}
return x;
};
i64 answer = inf;
for (int i = s; i < n; ++i) {
if (imos2[i] == 0) {
i64 v = baseCost;
v += 2 * solveL(i);
v += 2 * solveR(i);
const int re = revP[i];
v += 2 * std::min(std::abs(re - s), std::abs(s - i));
answer = std::min(answer, v);
break;
}
}
for (int i = s; i >= 0; --i) {
if (imos2[i] == 0) {
i64 v = baseCost;
v += 2 * solveL(i);
v += 2 * solveR(i);
const int re = revP[i];
v += 2 * std::min(std::abs(re - s), std::abs(s - i));
answer = std::min(answer, v);
break;
}
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
228 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
228 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
140 ms |
27848 KB |
Output is correct |
31 |
Correct |
158 ms |
27788 KB |
Output is correct |
32 |
Correct |
103 ms |
27784 KB |
Output is correct |
33 |
Correct |
112 ms |
27828 KB |
Output is correct |
34 |
Correct |
115 ms |
27792 KB |
Output is correct |
35 |
Correct |
113 ms |
27872 KB |
Output is correct |
36 |
Correct |
106 ms |
27824 KB |
Output is correct |
37 |
Correct |
106 ms |
27852 KB |
Output is correct |
38 |
Correct |
108 ms |
27848 KB |
Output is correct |
39 |
Correct |
140 ms |
27844 KB |
Output is correct |
40 |
Correct |
105 ms |
27880 KB |
Output is correct |
41 |
Correct |
122 ms |
27784 KB |
Output is correct |
42 |
Correct |
122 ms |
27872 KB |
Output is correct |
43 |
Correct |
109 ms |
27804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
228 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
140 ms |
27848 KB |
Output is correct |
31 |
Correct |
158 ms |
27788 KB |
Output is correct |
32 |
Correct |
103 ms |
27784 KB |
Output is correct |
33 |
Correct |
112 ms |
27828 KB |
Output is correct |
34 |
Correct |
115 ms |
27792 KB |
Output is correct |
35 |
Correct |
113 ms |
27872 KB |
Output is correct |
36 |
Correct |
106 ms |
27824 KB |
Output is correct |
37 |
Correct |
106 ms |
27852 KB |
Output is correct |
38 |
Correct |
108 ms |
27848 KB |
Output is correct |
39 |
Correct |
140 ms |
27844 KB |
Output is correct |
40 |
Correct |
105 ms |
27880 KB |
Output is correct |
41 |
Correct |
122 ms |
27784 KB |
Output is correct |
42 |
Correct |
122 ms |
27872 KB |
Output is correct |
43 |
Correct |
109 ms |
27804 KB |
Output is correct |
44 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
45 |
Halted |
0 ms |
0 KB |
- |