This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
long long minimum_walk(vector<int> p, int s) {
long long int ans = 0, n = p.size();
for (int i = 0; i < n; i++) {
ans += abs(i-p[i]);
}
vector<pair<int, int>> cycles;
for (int i = 0; i < n; i++) {
if (p[i] == i) {
continue;
}
int a = i, l = i, r = i;
a = p[i];
l = min(l, a);
r = max(r, a);
while(a != i) {
a = p[a];
l = min(l, a);
r = min(r, a);
}
cycles.push_back({l, r});
}
vector<int> pref(n+1, 0), f1(n, 0), f2(n, 0);
for (auto [i, j] : cycles) {
pref[i] += 1;
pref[j+1] -= 1;
}
for (int i = 1; i < n; i++) {
pref[i] += pref[i-1];
}
for (int i = n-1; i >= 0; i--) {
if (pref[i] > 0) {
f2[i] = 1;
}
if (i < n-1) {
f2[i] |= f2[i+1];
}
}
for (int i = n-1; i >= 0; i--) {
if (f2[i] && pref[i] == 0) {
ans += 2;
}
}
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... |