# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1042190 | raphaelp | Ancient Books (IOI17_books) | C++17 | 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>
using namespace std;
int64_t minimum_walk(int[] p, int S)
{
int N=p.size();
int64_t ans=0;
for (int i=0; i<N; i++)
{
ans+=abs(i-p[i]);
}
vector<int>occ(N);
for(int i=0; i<N; i++)
{
if(occ[i])continue;
int x=i;
ans+=2;
while(!occ[x])
{
occ[x]=1;
x=p[x];
}
return ans-2;
}