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;
int n;
long long minimum_walk(std::vector<int> p, int s) {
n = p.size();
vector <int> sz(n) ,vis(n) ,rep(n);
for(int i=0; i<n; i++) if(!vis[i]){
int tot = 0;
for(int j=i; !vis[j]; j=p[j]){
tot += abs(p[j]-j);
rep[j] = i;
vis[j] = 1;
}
sz[i] = tot;
}
fill(vis.begin() ,vis.end() ,0);
long long ans = 0LL ,lst = 0;
for(int i=0; i<n; i++){
if(!vis[rep[i]] && sz[rep[i]]){
ans += sz[rep[i]]+(i-lst);
vis[rep[i]] = 1;
lst = rep[i];
}
}
return ans+lst;
}
# | 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... |