#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastIO cin.tie(0); ios::sync_with_stdio(false)
#define cerr if(0) cout
int n;
ll ans = 0;
int holding = -1;
void solve(vector<int> &a, int l, int r, bool right = true, bool first = true) {
cerr << l << ' ' << r << '\n';
for(int i = right?l:r; (right && i <= r) || (!right && i >= l); right?i++:i--) {
cerr << i << ' ' << a[i] << '\n';
if(right) {if((a[i] > holding && a[i] != i) || (i == r)) {
cerr << "swap: " << holding << ' ' << a[i] << '\n';
swap(holding, a[i]);
}} else if((a[i] < holding && a[i] != i && a[i] != 0) || (i == l)) {
cerr << "swap: " << holding << ' ' << a[i] << '\n';
swap(holding, a[i]);
}
if(right && i != r) ans++;
if(!right && i != l) ans++;
}
cerr << holding << '\n';
for(auto &i : a) cerr << i << ' ';
cerr << '\n';
cerr << ans << "\n\n";
if(l < r) {
ans++;
if(right) solve(a, l + first, r-1, false, false);
else solve(a, l+1 + first, r, true, false);
} else {
ans += l;
}
}
ll minimum_walk(vector<int> p, int s) {
n = size(p);
vector<bool> vis(n, false);
assert(s == 0);
int l = 0, r = n-1;
while(p[l] == l && l < r) l++;
while(p[r] == r && l < r) r--;
solve(p, l, r);
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... |