제출 #859602

#제출 시각아이디문제언어결과실행 시간메모리
859602boris_mihov밀림 점프 (APIO21_jumps)C++17
33 / 100
4091 ms5244 KiB
#include "jumps.h" #include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <stack> typedef long long llong; const int MAXN = 200000 + 10; const int MAXLOG = 20; const llong INF = 1e18; const int INTINF = 2e9; int n; int h[MAXN]; int firstR[MAXN]; int firstL[MAXN]; std::stack <int> st; void init(int N, std::vector <int> H) { n = N; for (int i = 0 ; i < n ; ++i) { h[i + 1] = H[i]; } h[0] = h[n + 1] = INTINF; st.push(0); for (int i = 1 ; i <= n ; ++i) { while (h[i] > h[st.top()]) { st.pop(); } firstL[i] = st.top(); st.push(i); } while (st.size()) st.pop(); st.push(n + 1); for (int i = n ; i >= 1 ; --i) { while (h[i] > h[st.top()]) { st.pop(); } firstR[i] = st.top(); st.push(i); } } int minimum_jumps(int a, int b, int c, int d) { a++; b++; c++; d++; int pos = -1; int maxC = 0; for (int i = c ; i <= d ; ++i) { maxC = std::max(maxC, h[i]); } for (int i = a ; i <= b ; ++i) { if (h[i] <= maxC && firstR[i] > b) { pos = i; break; } } if (pos == -1) { return -1; } int res = 1; while (firstR[pos] < c) { if (h[firstL[pos]] > maxC && h[firstR[pos]] > maxC) { return -1; } if (h[firstL[pos]] <= maxC && (firstR[pos] > d || h[firstL[pos]] > h[firstR[pos]])) { res++; pos = firstL[pos]; } else { res++; pos = firstR[pos]; } } if (firstR[pos] > d) return -1; return res; }
#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...