Submission #654676

#TimeUsernameProblemLanguageResultExecution timeMemory
654676Sam_a17Rainforest Jumps (APIO21_jumps)C++14
33 / 100
4042 ms5836 KiB
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cout << #x << " " << x << endl;

const int maxN = 2e5 + 10;
int h[maxN], n, pre[maxN], nxt[maxN];

void init(int N, std::vector<int> H) {
  n = N;
  for(int i = 0; i < N; i++) {
    h[i] = H[i];

    //
    nxt[i] = pre[i] = -1;
  }

  stack<pair<int, int>> st;
  for(int i = 0; i < N; i++) {
    while(!st.empty() && h[i] > st.top().first) {
      nxt[st.top().second] = i;
      st.pop();
    }
    st.push({h[i], i});
  }

  while(!st.empty()) st.pop();

  for(int i = N - 1; i >= 0; i--) {
    while(!st.empty() && h[i] > st.top().first) {
      pre[st.top().second] = i;
      st.pop();
    }
    st.push({h[i], i});
  }
}

int minimum_jumps(int A, int B, int C, int D) {
  queue<pair<int, int>> q;
  vector<bool> vis(n + 1);
  for(int i = A; i <= B; i++) {
    q.push({i, 0});
    vis[i] = true;
  }

  while(!q.empty()) {
    auto u = q.front();
    q.pop();

    if(u.first >= C && u.first <= D) {
      return u.second;
    }

    if(nxt[u.first] != -1 && !vis[nxt[u.first]]) {
      vis[nxt[u.first]] = true;
      q.push({nxt[u.first], u.second + 1});
    }

    if(pre[u.first] != -1 && !vis[pre[u.first]]) {
      vis[pre[u.first]] = true;
      q.push({pre[u.first], u.second + 1});
    }
  }

  return -1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...