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;
long long minimum_walk(vector<int>p, int s) {
int n = p.size();
vector<int>v(n);
for(int i = 0 ; i < n ; i++)
v[i] = i;
int last = 0;
long long ans = 0;
for(int i = 0 ; i < n ; i++) {
if(i != p[v[i]]) {
last = i;
int pos = i;
int save = v[i];
do {
int nx = p[save];
ans += abs(nx - pos);
swap(save, v[p[save]]);
pos = nx;
} while(pos != i);
}
}
ans += 2*last;
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... |