이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |