# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
124376 | vex | Ancient Books (IOI17_books) | C++14 | 0 ms | 0 KiB |
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(int x)
{
bio[x]=true;
long long tre=abs(x-p[x]);
if(!bio[p[x]])tre+=dfs(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(i);
last=i;
}
}
sol+=last;
return sol;
}