#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 - 1; 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);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '12' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '12' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '12' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '8', found: '12' |
4 |
Halted |
0 ms |
0 KB |
- |