#include<bits/stdc++.h>
#include "books.h"
using namespace std;
using ll = long long;
int here[2000001] , one , two , boro;
long long ans;
long long minimum_walk(vector<int> p, int s) {
int n = p.size();
for( int z = 0; z < n; z++ ) {
here[z] = 0;
}
one = s;
ans = 0;
boro = 0;
for( int z = s; z < n; z++ ) {
if( here[z] == 0 && p[z] != z ) {
//ans += (ll)(2) * (ll)(abs(one - z));
if( boro < z ) ans += 2;
two = z;
while(here[two] == 0 ) {
boro = max( boro , two );
here[two] = 1;
ans += (ll)(abs(two - p[two]));
two = p[two];
}
one = z;
}
}
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... |