Submission #744748

#TimeUsernameProblemLanguageResultExecution timeMemory
744748t6twotwoRainforest Jumps (APIO21_jumps)C++17
4 / 100
1041 ms35604 KiB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> H, T;
bool subtask1;
vector<vector<int>> adj, par;
void init(int n, vector<int> h) {
    N = n, H = h;
    subtask1 = 1;
    for (int i = 0; i < N; i++) {
        H[i]--;
        if (i != H[i]) {
            subtask1 = 0;
        }
    }
    adj.resize(N);
    vector<int> stk;
    for (int i = N - 1; i >= 0; i--) {
        while (!stk.empty() && H[stk.back()] < H[i]) {
            stk.pop_back();
        }
        if (!stk.empty()) {
            adj[H[i]].push_back(H[stk.back()]);
        }
        stk.push_back(i);
    }
    stk.clear();
    for (int i = 0; i < N; i++) {
        while (!stk.empty() && H[stk.back()] < H[i]) {
            stk.pop_back();
        }
        if (!stk.empty()) {
            adj[H[i]].push_back(H[stk.back()]);
        }
        stk.push_back(i);
    }
    T.resize(N, N);
    for (int i = 0; i < N; i++) {
        if (!adj[i].empty()) {
            T[i] = *max_element(adj[i].begin(), adj[i].end());
        }
    }
    par.resize(N + 1, vector<int>(18, N));
    for (int i = 0; i < N; i++) {
        par[i][0] = T[i];
    }
    for (int j = 0; j < 17; j++) {
        for (int i = 0; i < N; i++) {
            par[i][j + 1] = par[par[i][j]][j];
        }
    }
}
int minimum_jumps(int A, int B, int C, int D) {
    if (subtask1) {
        return C - B;
    }
    if (A == B && C == D) {
        int x = H[A];
        int y = H[C];
        int ans = 0;
        for (int i = 17; i >= 0; i--) {
            if (par[x][i] <= y) {
                ans++;
                x = par[x][i];
            }
        }
        if (x != y) {
            ans = -1;
        }
        return ans;
    }
    vector<int> dp(N, N);
    for (int i = A; i <= B; i++) {
        dp[H[i]] = 0;
    }
    for (int i = 0; i < N; i++) {
        for (int j : adj[i]) {
            dp[j] = min(dp[j], dp[i] + 1);
        }
    }
    int ans = N;
    for (int i = C; i <= D; i++) {
        ans = min(ans, dp[H[i]]);
    }
    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...