# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
962918 | BhavayGoyal | Rainforest Jumps (APIO21_jumps) | C++14 | 0 ms | 0 KiB |
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>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 2e5+5;
int n, arr[N], Next[N], Prev[N];
void init(int nn, vi &h) {
n = nn;
for (int i = 1; i <= n; i++) arr[i] = h[i-1];
stack<int> st;
for (int i = 1; i <= n; i++) {
while (st.size() && arr[st.top()] < arr[i]) st.pop();
if (st.size()) Prev[i] = st.top();
else Prev[i] = -1;
st.push(i);
}
while (st.size()) st.pop();
for (int i = n; i >= 1; i--) {
while (st.size() && arr[st.top()] < arr[i]) st.pop();
if (st.size()) Next[i] = st.top();
else Next[i] = -1;
st.push(i);
}
}
int minimum_jumps(int a, int b, int c, int d) {
a++; b++; c++; d++;
queue<int> q;
vi dis(n+1, inf), vis(n+1, 0);
for (int i = a; i <= b; i++) {
q.push(i);
dis[i] = 0;
}
while (!q.empty()) {
int f = q.front(); q.pop();
if (f >= c && f <= d) return dis[f];
if (vis[f]) continue;
vis[f] = true;
int ch = Next[f];
if (ch != -1 && dis[ch] > dis[f]+1) {
dis[ch] = dis[f]+1;
q.push(ch);
}
ch = Prev[f];
if (ch != -1 && dis[ch] > dis[f]+1) {
dis[ch] = dis[f]+1;
q.push(ch);
}
}
return -1;
}