#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;
int m = 0;
for (int i = s; i < n; ++i) {
if (i != p[i]) {
i64 v = baseCost;
v += 2 * solveL(i);
v += 2 * solveR(i);
answer = v;
m += i - s;
break;
}
}
for (int i = s; i >= 0; --i) {
if (i != p[i]) {
m += s - i;
break;
}
}
if (imos2[s] == 0) m = 0;
return answer + m;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |