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 <cmath>
#include <iostream>
using namespace std;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using ll = long long;
int const nmax = 1000000;
int sumright[1 + nmax], sumleft[1 + nmax];
long long minimum_walk(std::vector<int> p, int start) {
int n = p.size();
ll result = 0;
for(int i = 0; i < n; i++){
result += fabs(p[i] - i);
if(i < p[i]){
sumright[i]++;
sumright[p[i]]--;
} else if(p[i] < i){
sumleft[i - 1]++;
if(0 <= p[i])
sumleft[p[i] - 1]--;
}
}
for(int i = 1; i < n; i++)
sumright[i] += sumright[i - 1];
for(int i = n - 1; 0 <= i; i--)
sumleft[i] += sumleft[i + 1];
int active = 0;
for(int i = n; start <= i; i--){
active |= (0 < sumright[i]);
active |= (0 < sumleft[i]);
if(0 == sumright[i] && 0 == sumleft[i])
result += active * 2;
else
result += fabs(sumright[i] - sumleft[i]);
}
active = 0;
for(int i = 0; i < start; i++){
active |= (0 < sumright[i]);
active |= (0 < sumleft[i]);
if(0 == sumright[i] && 0 == sumleft[i])
result += active * 2;
else
result += fabs(sumright[i] - sumleft[i]);
}
return result;
}
/*
6 0
0 1 2 5 4 3
*/
# | 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... |