제출 #1216624

#제출 시각아이디문제언어결과실행 시간메모리
1216624tapilyocaRainforest Jumps (APIO21_jumps)C++20
0 / 100
4066 ms20228 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using vec = vector<T>;
using ll = long long;


ll n;
vec<int> a;
vec<int> jumpRight, jumpLeft;
vec<vec<int>> up;

void init(int N, std::vector<int> H) {
    n = N;
    a = H;
    jumpRight.assign(n,-1);
    jumpLeft.assign(n,-1);
    up.assign(n,vec<int>(18,-1));

    stack<int> st;
    for(int i = 0; i < n; ++i) {
        while(!st.empty() and a[st.top()] < a[i]) {
            jumpRight[st.top()] = i;
            st.pop();
        }
        st.push(i);
    }

    while(!st.empty()) st.pop();
    for(int i = n - 1; i >= 0; --i) {
        while(!st.empty() and a[st.top()] < a[i]) {
            jumpLeft[st.top()] = i;
            st.pop();
        }
        st.push(i);
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    assert(A == B && C == D);


    int at = A;
    int jumpLimit = a[C];
    int ans = 0;
    while(at != C) {
        ans++;
        int lj = jumpLeft[at], rj = jumpRight[at];
        if(lj < jumpLimit and rj < jumpLimit) {
            if(lj >= rj) at = lj;
            else at = rj;
        } 
        else if(lj < jumpLimit and rj >= jumpLimit) at = lj;
        else if(rj < jumpLimit and lj >= jumpLimit) at = rj;
        else break;
    }
    return (at == C ? ans : -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...