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 maxn 1000005
using namespace std;
bool bio[maxn];
long long dfs(vector<int>p, int x)
{
bio[x]=true;
long long tre=abs(x-p[x]);
if(!bio[p[x]])tre+=dfs(p,p[x]);
return tre;
}
long long minimum_walk(vector<int>p, int s)
{
int n=p.size();
for(int i=0;i<n;i++)bio[i]=false;
long long sol=0LL;
int last=0;
for(int i=0;i<n;i++)
{
if(!bio[i])
{
sol+=dfs(p,i);
last=i;
}
}
sol+=2*last;
return sol;
}
# | 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... |