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>
using namespace std;
#include "books.h"
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
vector<bool> vis(n,false);
long long ans = 0;
int lel = -1;
for (int i = 0; i < n; ++i)
{
if (vis[i]) continue;
if (p[i]!=i){
if (lel==-1){
ans+=i*2;
}
else {
ans+=(i-lel-1)*2;
}
lel=i;
}
int node = i;
while (!vis[node]){
vis[node]=true;
ans+=abs(node-p[node]);
node=p[node];
}
}
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... |