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>
using namespace std;
#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 3e5;
ll minimum_walk(vector<int> p, int s) {
int n = sz(p), mx = 0;
vector<int> was(n + 1);
ll ans = 0;
for (int i = 0; i < n; i++) {
if (was[i] || i == p[i]) continue;
int x = i, mn = n;
while (!was[x]) {
mn = min(mn, x);
was[x] = 1;
ans += abs(p[x] - x);
x = p[x];
}
mx = max(mx, mn);
}
ans += (mx - s) * 2;
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... |