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>
#define pb push_back
int n,m,l,r,i,j,ii,jj,k,h[21][200002],subtask;
vector<int> v[200006];
int asuu(int l, int r){
int k=r-l+1;
k = 31 - __builtin_clz(k);
return max(h[k][l],h[k][r-(1<<k)+1]);
}
void init(int N, std::vector<int> H){
n=N;
for(i=0;i<n;i++){
h[0][i]=H[i];
if(H[i]!=i+1) subtask=1;
}
for(j=1;j<=20;j++)
for(i=0;i<n;i++){
l=h[j-1][i];
if(i+(1<<(j-1))<n) h[j][i]=max(l,h[j-1][i+(1<<(j-1))]);
}
for(i=0;i<n;i++){
if(i!=0 && asuu(0,i-1)>H[i]){
l=0;
r=i-1;
while(r-l>1){
m=(r+l)/2;
if(asuu(m+1,r)>H[i]) l=m+1;
else r=m;
}
if(H[r]>H[i]) k=r;
else k=l;
v[i].pb(k);
}
if(i!=n-1 && asuu(i+1,n-1) > H[i]){
l=i+1;
r=n-1;
while(r-l>1){
m=(r+l)/2;
if(asuu(l,m)>H[i]) r=m;
else l=m+1;
}
if(H[l]>H[i]) k=l;
else k=r;
v[i].pb(k);
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
if(subtask==0){
if(A>D) return -1;
else return C-B;
}
int dist[200005];
queue<int> q;
memset(dist,-1,sizeof(dist));
for(i=A;i<=B;i++){
q.push(i);
dist[i]=0;
}
while(!q.empty()){
int node= q.front();
q.pop();
for(i=0;i<v[node].size();i++)
if(dist[v[node][i]]==-1){
dist[v[node][i]]=dist[node]+1;
if(v[node][i]>=C && v[node][i]<=D) return dist[v[node][i]];
q.push(v[node][i]);
}
}
return -1;
}
Compilation message (stderr)
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:70:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(i=0;i<v[node].size();i++)
| ~^~~~~~~~~~~~~~~
# | 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... |