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 <cstdio>
#include <vector>
#include <cassert>
#include <cstdlib>
using namespace std;
#include <vector>
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size(), prevI = 0;
long long total = 0;
std::vector<int> v(n,0);
for(int i=0; i<n; i++) {
if (i==p[i]) continue;
total += i - prevI;
// printf("i=%d, prevI=%d, i-prevI=%d, total=%d\n",i,prevI,i-prevI,total);
prevI = i;
total += abs(p[i]-i);
// printf("i=%d, p[%d]=%d, abs(p[%d]-%d)=%d, total=%d\n",i,i,p[i],i,i,abs(p[i]-i),total);
int idx=p[i]; p[i]=i;
while(idx!=i) {
total += abs(p[idx]-idx);
// printf("idx=%d, p[%d]=%d, abs(p[%d]-%d)=%d, total=%d\n",idx,idx,p[idx],idx,idx,abs(p[idx]-idx),total);
int prevIdx = idx;
idx = p[idx];
p[prevIdx] = prevIdx;
}
// printf("prevI=%d\n",prevI);
}
return total + prevI;
}
# | 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... |