Submission #553539

#TimeUsernameProblemLanguageResultExecution timeMemory
553539Jarif_RahmanRainforest Jumps (APIO21_jumps)C++17
0 / 100
4040 ms4168 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; int n; vector<int> h; vector<int> jump[2]; void init(int N, vector<int> H){ n = N, h = H; fill(jump, jump+2, vector<int>(n, -1)); stack<int> st; for(int i = n-1; i >= 0; i--){ while(!st.empty() && h[i] > h[st.top()]){ jump[0][st.top()] = i; st.pop(); } st.push(i); } while(!st.empty()) st.pop(); for(int i = 0; i < n; i++){ while(!st.empty() && h[i] > h[st.top()]){ jump[1][st.top()] = i; st.pop(); } st.push(i); } while(!st.empty()) st.pop(); } int minimum_jumps(int A, int B, int C, int D){ vector<int> dis(n, n+1); vector<bool> bl(n, 0); queue<int> Q; for(int i = A; i <= B; i++){ dis[i] = 0; bl[i] = 1; Q.push(i); } while(!Q.empty()){ auto nd = Q.front(); Q.pop(); if(jump[0][nd] != -1 && !bl[jump[0][nd]]){ bl[jump[0][nd]]; dis[jump[0][nd]] = dis[nd]+1; Q.push(jump[0][nd]); } } int ans = *min_element(dis.begin()+C, dis.begin()+D+1); fill(dis.begin(), dis.end(), n+1); fill(bl.begin(), bl.end(), 0); for(int i = A; i <= B; i++){ dis[i] = 0; bl[i] = 1; Q.push(i); } while(!Q.empty()){ auto nd = Q.front(); Q.pop(); if(jump[1][nd] != -1 && !bl[jump[1][nd]]){ bl[jump[1][nd]]; dis[jump[1][nd]] = dis[nd]+1; Q.push(jump[1][nd]); } } ans = min(ans, *min_element(dis.begin()+C, dis.begin()+D+1)); if(ans > n) ans = -1; return ans; }
#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...