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 <bits/stdc++.h>
#include "books.h"
using namespace std;
long long minimum_walk(vector<int> p, int s){
int n = p.size();
vector<long long> arr(n + 1);
for(int i = 0; i < n; i++){
arr[min(i, p[i])]++;
arr[max(i, p[i])]--;
}
for(int i = 1; i < n; i++) arr[i] += arr[i - 1];
int mn = n, mx = -1;
for(int i = 0; i < n; i++) if(arr[i]) mn = min(mn, i), mx = max(mx, i);
if(mx == -1) return 0;
else return accumulate(arr.begin() + mn, arr.begin() + mx + 1, 0ll) + 2 * count(arr.begin() + mn, arr.begin() + mx + 1, 0) + min(2 * abs(s - mn), 2 * abs(s - mx));
return 0;
}
# | 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... |