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 vector<int> vi;
typedef long long ll;
#define sz(x) (int)(x).size()
long long minimum_walk(vi p, int s) {
bool all_good = 1;
for(int i = 0; i < sz(p); i++){
if(p[i] != i) all_good = 0;
}
if(all_good) return 0;
int n = sz(p);
vi cover = vi(n, 0);
vi dirover = vi(n, 0); //right = +1
ll ans = 0;
for(int i = 0; i < n; i++){
cover[min(i, p[i])]++;
cover[max(i, p[i])]--;
dirover[i]++;
dirover[p[i]]--;
ans+=abs(p[i]-i);
}
//cout << ans << "\n";
for(int i = 1; i < n; i++){
cover[i]+=cover[i-1];
dirover[i]+=dirover[i-1];
}
for(int i = 0; i < n; i++){
ans+=abs(dirover[i]);
}
int L = 0;
int R = n-2;
for(int i = 0; i <= s-1; i++){
if(p[i] == i){
L = i+1;
}
else break;
}
for(int i = n-2; i >= s; i--){
if(p[i+1] == i+1){
R = i-1;
}
else break;
}
for(int i = L; i <= R; i++){
if(cover[i] == 0){
ans+=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... |