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 <bits/stdc++.h>
#include "books.h"
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
vector<bool> vis(n);
vector<long long> cost(n);
vector<int> to(n);
long long ans = 0;
for (int i = 0; i < n; i++) if (i != p[i] && !vis[i]) {
int right = i;
int tmp = i;
while (!vis[tmp]) {
ckmax(right, tmp);
ans += abs(tmp - p[tmp]), vis[tmp] = 1, tmp = p[tmp];
}
to[i] = right;
}
int on = 0;
fill(all(vis), 0);
for (int i = 0; i < n; i++) if (i != p[i] && !vis[i]) {
int tmp = i;
while (!vis[tmp]) vis[tmp] = 1, tmp = p[tmp];
if (i <= on) ckmax(on, to[i]);
else ans += 2 * (i - on), on = to[i];
}
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... |