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;
typedef long long ll;
const ll N = 1e6;
const ll inf = 1e18;
ll ps[N + 1];
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
ll ans = 0;
ll mx = -inf;
ll mn = inf;
ll mx2 = -inf;
ll mn2 = inf;
bool ok = 0;
for(ll i = 0;i < n;i++){
ans+=abs(i - p[i]);
if(i != p[i]){
ok = 1;
if(i < p[i]){
ps[p[i]]--;
ps[i]++;
}
if(i <= s){
mx = max(mx,i);
}
if(i >= s){
mn = min(mn,i);
}
mx2 = max(mx2,i);
mn2 = min(mn2,i);
}
}
if(ok == 0)return ans;
ll sum = 0;
for(int i = mn2;i < mx2;i++){
sum+=ps[i];
if(sum == 0)ans+=2;
}
if(mx == -inf || mn == inf){
ans+=2*min(s - mx,mn - s);
}
return ans;
}
/**
7 3
2 1 0 3 6 5 4
**/
# | 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... |