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"
#define rep(i,n) for(int i = 0; i < n; i++)
#define lint long long
using namespace std;
vector<int>P;
vector<bool>vis;
int dfs(int v)
{
if(vis[v]) return 0;
vis[v]=true;
return abs(v - P[v]) + dfs(P[v]);
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
while(p[0]==2||p[0]==1){
s++;
}
vis.resize(n);
P=p;
int count=0;
int numComp=0;
rep(i, n)
{
if(vis[i]){continue;}
numComp++;
count+=dfs(i);
}
return count+2*(numComp-1);
}
# | 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... |