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<queue>
#include <vector>
#include <iostream>
using namespace std;
std::vector<int> H;
vector<int> nxt;
vector<int> prv;
void init(int N, std::vector<int> nH)
{
H = nH;
nxt.resize(N);
prv.resize(N);
{
vector<int> kmon;
for (int i = 0; i < N; i++)
{
if (kmon.empty())
{
kmon.push_back(i);
prv[i] = -1;
continue;
}
while (H[kmon.back()] < H[i])
{
kmon.pop_back();
if (kmon.empty())
{
kmon.push_back(i);
prv[i] = -1;
goto nd;
}
}
prv[i] = kmon.back();
kmon.push_back(i);
nd:;
}
}
{
vector<int> kmon;
for (int i = N-1; i>=0; i--)
{
if (kmon.empty())
{
kmon.push_back(i);
nxt[i] = -1;
continue;
}
while (H[kmon.back()] < H[i])
{
kmon.pop_back();
if (kmon.empty())
{
kmon.push_back(i);
nxt[i] = -1;
goto nd2;
}
}
nxt[i] = kmon.back();
kmon.push_back(i);
nd2:;
}
}
}
int minimum_jumps(int A, int B, int C, int D)
{
int mcd = 0;
for (int i = C; i <= D; i++)
{
mcd = max(mcd, H[i]);
}
int bs = 0;
for (int i = B; i >= A; i--)
{
// cerr << H[i] << ' ';
if (H[i] < mcd)
bs = max(bs, i);
else
break;
}
// cerr << bs << '\n';
vector<bool> visited(H.size(), 0);
queue<pair<int, int>> q;
for(int i = A; i <=B; i++)
q.push({i, 0});
while(!q.empty()){
auto x = q.front();
q.pop();
if(visited[x.first]) continue;
visited[x.first] = 1;
if(x.first >= C && x.first <= D) return x.second;
if(nxt[x.first]!=-1) q.push({nxt[x.first], x.second+1});
if(prv[x.first]!=-1) q.push({prv[x.first], x.second+1});
}
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... |