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"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
vector<vector<ll>>PD,PI;
vector<vector<ll>> cad;
vector<ll> vD;
void dfs(ll node, ll d)
{
vD[node]=d;
for(auto i:cad[node])
dfs(i,d+1);
}
void init(int N, std::vector<int> h)
{
n=N; stack<ll> q;
PD.assign(24,vector<ll>(n+6,n+5));
PI.assign(24,vector<ll>(n+6,-1));
cad.resize(n+6);vD.resize(n+6);
for(int i=0; i<n; i++)
{
while(!q.empty()&&h[i]>h[q.top()])
{
PD[0][q.top()]=i;
q.pop();
}
if(!q.empty()) PI[0][i]=q.top();
q.push(i);
}
for(int i=0; i<n; i++)
cad[PD[0][i]].push_back(i);
for(int i=1; i<24; i++)
for(int j=0; j<n+6; j++)
PD[i][j]=PD[i-1][PD[i-1][j]];
dfs(n+5,0);
}
ll sol(ll node, ll a, ll b)
{
ll c=0;
for(int i=23; i>-1; i--)
{
if(PD[i][node]<a)
{
c+=(1<<i);
node=PD[i][node];
}
}
node=PD[0][node];
if(node>b) return 1e9;
return c+1;
}
int minimum_jumps(int A, int B, int C, int D)
{
ll c1=0;
ll p=B;
ll bs=1e9;
while(p!=-1)
{
bs=min(bs,c1+sol(p,C,D));
p=PI[0][p];
if(p<A) c1++;
}
if(bs==1e9) bs=-1;
return bs;
}
# | 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... |