Submission #1183263

#TimeUsernameProblemLanguageResultExecution timeMemory
1183263thangdz2k7Rainforest Jumps (APIO21_jumps)C++20
0 / 100
4067 ms5412 KiB
#ifdef EVAL

#include "jumps.h"

#endif

#include <bits/stdc++.h>

using namespace std;

const int MAX = 2e5 + 5;

int n, h[MAX], l[MAX], r[MAX];

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

    r[0] = n + 1;
    vector <int> stk;
    for (int i = 1; i <= n; ++ i){
        while (stk.size() && h[i] > h[stk.back()])
            stk.pop_back();

        l[i] = stk.size() ? stk.back() : 0;
        stk.push_back(i);
    }

    stk.clear();
    for (int i = n; i >= 1; -- i){
        while (stk.size() && h[i] > h[stk.back()])
            stk.pop_back();

        r[i] = stk.size() ? stk.back() : n + 1;
        stk.push_back(i);
    }
}

int minimum_jumps(int a, int b, int c, int d){
    a ++, b ++, c ++, d ++;
    int st = 0;
    for (int i = a; i <= b; ++ i) if (r[i] <= d) {
        if (!st || h[i] > h[st])
            st = i;
    }

    int ans = 0;
    while (st < c && r[st] <= d){
        int i = l[st], j = r[st];
        if (h[i] < h[j]) swap(i, j);
        ans ++;
        if (c <= i && i <= d) return ans;
        if (c <= j && j <= d) return ans;
        if (r[i] <= d) st = i;
        else st = j;
    }

    if (st >= c) return ans;
    return -1;
}

/*
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2
*/
#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...