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;
vector<int> P;
int vis[1001];
int sz = 0;
void dfs(int i){
vis[i] = 1;
sz++;
if(!vis[P[i]])dfs(P[i]);
}
long long minimum_walk(vector<int> p, int s){
int ans = 0;
int n = p.size();
P.clear();
for(int i = 0;i<n;i++){
vis[i] = 0;
P.push_back(p[i]);
ans+=abs(i-p[i]);
}
vector<int> lol;
for(int i = 0;i<n;i++){
if(!vis[i]){
sz = 0;
dfs(i);
if(i==0||sz>1)lol.push_back(i);
}
}
int sz = lol.size();
ans+=(sz-1)*2;
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... |