#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);
for (int i = 0; i < n; ++i) {
int a = i, b = p[i];
if (a > b) std::swap(a, b);
++imos[a];
--imos[b];
}
for (int i = 0; i < n - 1; ++i) imos[i + 1] += imos[i];
i64 baseCost = std::accumulate(imos.begin(), imos.end(), 0ll);
auto solve = [&](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;
}
}
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;
};
const auto u = solve(s);
i64 m = inf;
for (int i = s; i < n; ++i) {
if (i == n - 1 or imos[i + 1] == 0) {
m = std::min(m, (i64)i - s);
}
}
for (int i = s; i >= 0; --i) {
if (i == 0 or imos[i - 1] == 0) {
m = std::min(m, (i64)s - i);
}
}
return baseCost + 2 * (solve(s) + (imos[s] == 0 ? 0 : m));
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:46:16: warning: unused variable 'u' [-Wunused-variable]
46 | const auto u = solve(s);
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
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 |
107 ms |
15852 KB |
Output is correct |
31 |
Correct |
97 ms |
15980 KB |
Output is correct |
32 |
Correct |
87 ms |
15860 KB |
Output is correct |
33 |
Correct |
105 ms |
15856 KB |
Output is correct |
34 |
Correct |
100 ms |
15952 KB |
Output is correct |
35 |
Correct |
97 ms |
15868 KB |
Output is correct |
36 |
Correct |
98 ms |
15852 KB |
Output is correct |
37 |
Correct |
97 ms |
15856 KB |
Output is correct |
38 |
Correct |
92 ms |
15864 KB |
Output is correct |
39 |
Correct |
91 ms |
15860 KB |
Output is correct |
40 |
Correct |
90 ms |
15848 KB |
Output is correct |
41 |
Correct |
90 ms |
15852 KB |
Output is correct |
42 |
Correct |
95 ms |
15864 KB |
Output is correct |
43 |
Correct |
92 ms |
15848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
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 |
1 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 |
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 |
107 ms |
15852 KB |
Output is correct |
31 |
Correct |
97 ms |
15980 KB |
Output is correct |
32 |
Correct |
87 ms |
15860 KB |
Output is correct |
33 |
Correct |
105 ms |
15856 KB |
Output is correct |
34 |
Correct |
100 ms |
15952 KB |
Output is correct |
35 |
Correct |
97 ms |
15868 KB |
Output is correct |
36 |
Correct |
98 ms |
15852 KB |
Output is correct |
37 |
Correct |
97 ms |
15856 KB |
Output is correct |
38 |
Correct |
92 ms |
15864 KB |
Output is correct |
39 |
Correct |
91 ms |
15860 KB |
Output is correct |
40 |
Correct |
90 ms |
15848 KB |
Output is correct |
41 |
Correct |
90 ms |
15852 KB |
Output is correct |
42 |
Correct |
95 ms |
15864 KB |
Output is correct |
43 |
Correct |
92 ms |
15848 KB |
Output is correct |
44 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |