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;
long long res;
long long minimum_walk(std::vector<int> p, int s) {
while(p.back() == p.size() - 1)
p.pop_back();
int n = p.size();
if(s >= n){
res += (s - n + 1) << 1;
s = n - 1;
}
reverse(p.begin(), p.end());
s = n - 1 - s;
for(int i = 0; i < n; i++)
p[i] = n - 1 - s;
while(p.back() == p.size() - 1)
p.pop_back();
n = p.size();
if(s >= n){
res += (s - n + 1) << 1;
s = n - 1;
}
int tmn = s, tmx = s;
for(int i = 0; i < n; i++){
res += abs(p[i] - i);
if(1LL * (tmx - p[i]) * (tmx - i) <= 0 || 1LL * (tmn - p[i]) * (tmn - i) <= 0){
tmx = max(tmx, max(i, p[i]));
tmn = min(tmn, min(i, p[i]));
}
}
int l, r, mn, mx;
l = mn = min(s, p[s]);
r = mx = max(s, p[s]);
while(mn != tmn || mx != tmx){
res += 2;
mn--;
mx++;
while(l != mn || r != mx){
if(l != mn){
l--;
mn = min(mn, p[l]);
mx = max(mx, p[l]);
} else {
r++;
mn = min(mn, p[r]);
mx = max(mx, p[r]);
}
}
}
for(; l >= 0; l--){
if(mn > l) res += 2;
mn = min(mn, p[l]);
}
for(; r < n; r++){
if(mx < r) res+= 2;
mx = max(mx, p[r]);
}
return res;
}
Compilation message (stderr)
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:6:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(p.back() == p.size() - 1)
~~~~~~~~~^~~~~~~~~~~~~~~
books.cpp:19:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(p.back() == p.size() - 1)
~~~~~~~~~^~~~~~~~~~~~~~~
# | 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... |