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) {
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]);
}
for(int i = 0; i < n-1; 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... |