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;
const int INF = 1e9 + 1;
vector<int> firstGreaterToTheRight;
vector<vector<int>> adj;
void init(int N, vector<int> H)
{
stack<pair<int, int>> s;
firstGreaterToTheRight = vector<int>(N, -1);
adj = vector<vector<int>>(N);
for (int i = N - 1; i >= 0; i--)
{
while (!s.empty() && s.top().first < H[i])
s.pop();
if (!s.empty())
firstGreaterToTheRight[i] = s.top().second;
s.push({H[i], i});
}
for (int i = 0; i < N; i++)
if (firstGreaterToTheRight[i] != -1)
adj[firstGreaterToTheRight[i]].push_back(i);
}
int minimum_jumps(int A, int B, int C, int D)
{
int res = INF;
for (int R = C; R <= D; R++)
{
queue<pair<int, int>> bfs;
map<int, bool> vis;
bfs.push({R, 0});
vis[R] = true;
while (!bfs.empty())
{
pair<int, int> cur = bfs.front();
bfs.pop();
for (int a : adj[cur.first])
{
if (vis[a])
continue;
bfs.push({a, cur.second + 1});
if (A <= a && a <= B)
res = min(res, cur.second + 1);
}
}
}
return (res == INF ? -1 : res);
}
# | 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... |