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;
long long minimum_walk(std::vector<int> p, int s) {
assert(s == 0);
long long ans = 0, n = p.size(), stt = 0;
set<int> st;
map<int, int> mp;
vector<int> b(n + 2);
for (int i = 0; i < n; i ++) {
if (p[i] != i) {
ans += abs(i - p[i]);
st.insert(p[i]);
mp[p[i]] ++;
mp[i] --;
st.erase(i);
}
if (st.size() == 0 || *st.rbegin() <= i) b[i] = 1;
}
for (int i = n - 2; i >= 0; i --) {
if (stt && b[i]) ans += 2;
if (p[i] != i) stt = 1;
}
return ans;
}
/*
4 0
3 2 1 0
*/
# | 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... |