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;
long long minimum_walk(vector<int>p, int s) {
int n = p.size();
long long ans = 0;
vector<bool>vis(n,0);
int mx = 0;
for(int i = 0 ; i < n ; i++) if(!vis[i] && p[i] != i) {
vis[i] = 1;
if(i > mx && p[i] != i)
ans += 2*(i-mx);
int pos = i;
do {
vis[pos] = 1;
mx = max(mx, pos);
int nxt = p[pos];
ans += abs(nxt-pos);
pos = nxt;
} while(pos != i);
}
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... |