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 "jumps.h"
//#include "stub.cpp"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
#define all(x) x.begin(),x.end()
#define pb push_back
vvi g;
int n=-1;
bool line=1;
void init(int N, vi h){
n=N;
g.resize(n);
for(int i=0; i<n; i++)
line&=h[i]==i+1;
vi nxt(n,-1),prv(n,-1);
stack<int>s;
for(int i=0; i<n; i++){
while(s.size() && h[i]>h[s.top()]){
nxt[s.top()]=i;
s.pop();
}
s.push(i);
}
while(s.size())
s.pop();
for(int i=n-1; 0<=i ;i--){
while(s.size() && h[i]>h[s.top()]){
prv[s.top()]=i;
s.pop();
}
s.push(i);
}
for(int i=0; i<n;i ++){
if(nxt[i]!=-1)
g[i].pb(nxt[i]);
if(prv[i]!=-1)
g[i].pb(prv[i]);
}
}
int minimum_jumps(int A, int B, int C, int D){
if(line)
return C-B;
vector<bool>vis(n);
queue<pi>q;
for(int i=A; i<=B; i++)
q.push({0,i});
while(q.size()){
pi cur=q.front();
q.pop();
int src=cur.second,dst=cur.first;
if(C<= src && src<=D)
return dst;
if(vis[src])continue;
vis[src]=1;
for(int nbr:g[src])
if(!vis[nbr])
q.push({dst+1,nbr});
}
return -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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |