# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
922182 | Arp | Rainforest Jumps (APIO21_jumps) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
const int N = 2e5;
int ml[N],mr[N];
int best[N];
int arr[N];
void init(int n,vector<int> a){
for(int i = 0;i<n;i++){
ml[i] = -1;
mr[i] = n;
arr[i] = a[i];
}
stack<int> lg;
for(int i = 0;i<n;i++){
while(lg.size() > 0 && arr[lg.top()] < arr[i]) lg.pop();
if(lg.size() > 0){
ml[i] = lg.top();
}
lg.push(i);
}
stack<int> rg;
for(int i = n - 1;i >= 0;i--){
while(rg.size() > 0 && arr[rg.top()] < arr[i]) rg.pop();
if(rg.size() > 0){
mr[i] = rg.top();
}
rg.push(i);
}
for(int i = 0;i<n;i++){
best[i] = (ml[i] > mr[i] ? ml[i] : mr[i]);
}
}
int minimum_jumps(int a,int b,int c,int d){
int m2 = b;
for(int i = c;i <= d;i ++){
if(arr[i] > arr[m2]) m2 = i;
}
int m1 = a;
for(int i = a;i <= b;i++){
if(arr[i] > arr[m1] && arr[i] < arr[m2]) m1 = i;
}
if(arr[m1] > arr[m2]) return -1;
int last = m1;
int ans = 0;
while(arr[best[last]] < arr[m2]){
last = best[last];
ans ++;
}
while(!(last >= c && last <= d)){
if(arr[last] > arr[m2]) return -1;
last = mr[last];
ans ++;
}
return ans;
}