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 "jumps.h"
using namespace std;
#include <bits/stdc++.h>
bool sb1 = true;
int n, dp[200005], ad[200005], a[200005];
int nxup[200005],nxls[200005],lsup[200005];
void init(int N, std::vector<int> H) {
n = N;
for(int i=0;i<n;++i) a[i] = H[i];
for(int i=1;i<n;++i){
if(a[i] < a[i-1]) sb1 = false;
}
vector<pair<int,int>> v;
for(int i=0;i<n;++i){
v.push_back({H[i],i});
}
sort(v.begin(),v.end(),greater<pair<int,int>>());
for(int i=0;i<n;++i) nxup[i] = nxls[i] = lsup[i] = -1;
set<int> s;
for(int i=0;i<n;++i) {
if(!s.empty()){
set<int>::iterator it= s.upper_bound(v[i].second);
if(it != s.end()){
nxup[v[i].second] = *it;
}
if(it != s.begin()){
--it;
lsup[v[i].second] = *it;
}
}
s.insert(v[i].second);
}
}
int minimum_jumps(int A, int B, int C, int D) {
if(sb1) return C - B;
if(A == B && C == D) {
if(a[C] < a[B]) return -1;
int res = 0;
for(;;){
if(B == C) break;
if(nxup[B] == -1 || B > C) {res = -1; break;}
++res;
int ls = lsup[B],nx = nxup[B];
if(ls != -1) {
if(a[ls] > a[nx] && a[ls] < a[C]) B = ls;
else B = nx;
}
else B = nx;
}
return res;
}
int res = 1e9;
for(int i=C;i<=D;++i) dp[i] = 0;
for(int i = A;i <= B; ++i) ad[i] = 0;
for(int i=0;i < A; ++i) ad[i] = 1e9;
for(int i=C-1;i>=0;i--){
int nx = nxup[i];
if(nx > D) dp[i] = 1e9;
else dp[i] = dp[nx] + 1;
if(i >= A && i <= B){
res = min(res,dp[i]);
}
int ls = lsup[i];
if(ls != -1){
ad[ls] = min(ad[ls], ad[i] + 1);
}
else if(i< A){
res = min(res,dp[i] + ad[i]);
}
}
if(res >= 1e9) return -1;
return res;
}
/*
7 5
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2
2 2 6 6
1 1 5 5
*/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |