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 "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
long long minimum_walk(vector<int> p, int s) {
ll ans = 0;
int n = p.size();
int best = 0;
vector<bool> taken(n, false);
for(int i = 0; i < n; i++) {
if(p[i] == i) continue;
if(best < i) {
ans += 2 * (i - best);
}
if(!taken[i]) {
int x = i;
while(!taken[x]) {
best = max(x, best);
taken[x] = true;
ans += abs(x - p[x]);
x = p[x];
}
}
}
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... |