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 ll long long
const int mxn = 1e6+10;
int N;
int l[mxn],r[mxn];
long long minimum_walk(std::vector<int> p, int s) {
ll ans = 0;
N = p.size();
for(int i = 0;i<N;i++){
if(p[i] == i)continue;
int a = i,b = p[i];
if(a<b){
l[a]++;
l[b]--;
}
else{
r[b]++;
r[a]--;
}
}
for(int i = 1;i<N;i++){
l[i] += l[i-1];
r[i] += r[i-1];
}
int lp = s,rp = s-1;
for(int i = s;i+1<N;i++){
if(max(l[i],r[i]) != 0)rp = i;
}
for(int i = s-1;i>=0;i--){
if(max(l[i],r[i]) != 0)lp = i;
}
for(int i = lp;i<=rp;i++){
ans += max({l[i],r[i],1})<<1;
}
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... |