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"
using namespace std;
typedef long long ll;
ll minimum_walk(vector<int> p, int s) {
ll n = p.size();
ll res = 0;
ll ooo = 0;
for (int i = 0; i < n; i++) ooo += (p[i] != i);
vector<bool> vst(n);
for (int i = 0; i < n && ooo > 0; i++) {
if (vst[i]) { res += 2; continue; }
ll cur = i;
do {
if (p[cur] != cur) ooo--;
res += abs(cur - p[cur]);
cur = p[cur];
vst[cur] = true;
} while (cur != i);
if (ooo > 0) res += 2;
}
return res;
}
# | 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... |