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;
#define ll long long
long long minimum_walk(std::vector<int> p, int s) {
if (s != 0) return 0; // save time
// get each cycle length and min.
int n = p.size();
bool settled[n];
for (int i=0; i<n; i++) settled[i] = false;
ll allmax = 0;
ll currmin;
ll cyclesum = 0;
for (ll ii=0; ii<n; ii++) {
if (settled[ii]) continue;
// now, find this cycle.
ll i = p[ii];
//ll prev = ii;
currmin = i;
settled[i] = true;
cyclesum += abs(ii-i);
currmin = min(ii, i);
while (i != ii) {
//cout << i << ' ' << p[i] << " -> ";
// do stuff within cycle
settled[i] = true;
cyclesum += abs(i-p[i]);
i = p[i];
currmin = min(currmin, i);
//cout << i << ' ' << p[i] << ": " << cyclesum << endl;
}
//cout << i << ' ' << ii << endl;
settled[ii] = true;
cyclesum += abs(ii-i);
currmin = min(currmin, ii);
allmax = max(currmin, allmax);
}
//cout << "CYCLESUM: " << cyclesum << ", ALLMAX: " << allmax << endl;
return cyclesum + 2*allmax;
}
# | 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... |