이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int first[N], last[N], cycles[N];
bool visited[N];
long long minimum_walk(vector<int> perm, int start) {
int n = perm.size();
long long ans = 0;
int low = start, high = start;
for (int i = 0; i < n; ++i) {
ans += abs(perm[i] - i);
if (!visited[i]) {
int j = i, k = i;
while (!visited[j]) {
visited[j] = true;
k = max(k, j);
j = perm[j];
}
do {
first[j] = i, last[j] = k;
j = perm[j];
} while (j != i);
if (i != k) {
low = min(low, i), high = max(high, k);
++cycles[i], --cycles[k];
}
}
}
partial_sum(cycles, cycles + n, cycles);
ans += 2 * count(cycles + low, cycles + high, 0);
bool init = false;
low = high = start;
while (cycles[high] > 0) {
int cost_h = 0, ptr_h = high, new_high = high;
int cost_l = 0, ptr_l = low, new_low = low;
if (init) {
while (first[ptr_h] >= low) {
new_high = max(new_high, last[ptr_h]);
if (ptr_h == new_high) {
cost_h += 2;
++new_high;
}
++ptr_h;
}
while (last[ptr_l] <= high) {
new_low = min(new_low, first[ptr_l]);
if (ptr_l == new_low) {
cost_l += 2;
--new_low;
}
--ptr_l;
}
}
ans += min(cost_h, cost_l);
while (new_low <= ptr_l || ptr_h <= new_high) {
int i = new_low <= ptr_l ? ptr_l-- : ptr_h++;
new_low = min(new_low, first[i]);
new_high = max(new_high, last[i]);
}
low = new_low, high = new_high, init = true;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |