답안 #671344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
671344 2022-12-12T21:54:41 Z YENGOYAN 밀림 점프 (APIO21_jumps) C++17
4 / 100
939 ms 51248 KB
#include "jumps.h"
#include <bits/stdc++.h>

using namespace std;

vector<int> l, r, h;
vector<vector<int>> upmec, uppoqr;
vector<bool> used;
bool f = 1;
int n;

void calcDp(int u, vector<int> &dp, int c, int d){
    if(dp[u] != -1) return;
    if(u >= c && u <= d) {
        dp[u] = 0;
        return;
    }
    if(l[u] != -1 && r[u] != -1) {
        calcDp(l[u], dp, c, d);
        calcDp(r[u], dp, c, d);
        dp[u] = min(dp[l[u]], dp[r[u]]) + 1;
    }
    else if(l[u] != -1) {
        calcDp(l[u], dp, c, d);
        dp[u] = dp[l[u]] + 1;
    }
    else if(r[u] != -1){
        calcDp(r[u], dp, c, d);
        dp[u] = dp[r[u]] + 1;
    }
    else dp[u] = 1e9;
}

void calcUp(int u){
    if(used[u]) return;
    used[u] = 1;
    if(r[u] != -1 && l[u] != -1){
        if(h[r[u]] > h[l[u]]){
            calcUp(r[u]), calcUp(l[u]);
            upmec[u][0] = r[u];
            uppoqr[u][0] = l[u];
            for(int i = 1; i < 20; i++){
                upmec[u][i] = upmec[upmec[u][i - 1]][i - 1];
                uppoqr[u][i] = uppoqr[uppoqr[u][i - 1]][i - 1];
            }
        }
        else{
            calcUp(r[u]), calcUp(l[u]);
            upmec[u][0] = l[u];
            uppoqr[u][0] = r[u];
            for(int i = 1; i < 20; i++){
                upmec[u][i] = upmec[upmec[u][i - 1]][i - 1];
                uppoqr[u][i] = uppoqr[uppoqr[u][i - 1]][i - 1];
            }
        }
    }
    else if(r[u] != -1){
        calcUp(r[u]);
        upmec[u][0] = uppoqr[u][0] = r[u];
        for(int i = 1; i < 20; i++){
            upmec[u][i] = uppoqr[u][i] = upmec[upmec[u][i - 1]][i - 1];
        }
    }
    else if(l[u] != -1){
        calcUp(l[u]);
        upmec[u][0] = uppoqr[u][0] = l[u];
        for(int i = 1; i < 20; i++){
            upmec[u][i] = uppoqr[u][i] = upmec[upmec[u][i - 1]][i - 1];
        }
    }
    else {
        for(int i = 0; i < 20; i++) uppoqr[u][i] = upmec[u][i] = -1;
    }
}

bool check(int n, int q){
    if(n <= 2000 && q <= 2000) return 1;
    if(q <= 5) return 1;
    return 1;
}

void init(int N, vector<int> H) {
    n = N, h = H;
    l = r = vector<int> (n, -1);
    upmec = uppoqr = vector<vector<int>> (n, vector<int> (20));
    used = vector<bool> (n);
    for(int i = 0; i < n; i++) if(h[i] != i + 1) f = 0;
    if(f) return;
    stack<pair<int, int>> st;
    st.push({1e9, -1});
    for(int i = n - 1; i >= 0; i--){
        while(st.top().first < h[i]){
            st.pop();
        }
        r[i] = st.top().second;
        st.push({h[i], i});
    }
    while(st.size())st.pop();
    st.push({1e9, -1});
    for(int i = 0; i < n; i++){
        while(st.top().first < h[i]){
            st.pop();
        }
        l[i] = st.top().second;
        st.push({h[i], i});
    }
    for(int i = 0; i < n; i++) {
        if(!used[i]) calcUp(i);
    }
//     up[i][j]
}

int minimum_jumps(int A, int B, int C, int D) {
    if(f) return C - B;
    int ans = 0;
    if(A == B && C == D){
        int node = A;
        for(int i = 19; i >= 0; i--){
            if(upmec[node][i] == -1) continue;
            if(h[upmec[node][i]] <= h[C]){
                ans += (1 << i);
                node = upmec[node][i];
            }
        }
        for(int i = 19; i >= 0; i--){
            if(uppoqr[node][i] == -1) continue;
            if(h[uppoqr[node][i]] <= h[C]){
                ans += (1 << i);
                node = uppoqr[node][i];
            }
        }
        if(node == C) return ans;
        else return -1;
    }
    vector<int> dp(n, -1);
    for(int i = A; i <= B; i++) calcDp(i, dp, C, D);
//    int ans = 2e9;
    for(int i = A; i <= B; i++){
        ans = min(ans, dp[i]);
    }
    if (ans >= 1e9) return -1;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 132 ms 40728 KB Output is correct
4 Correct 882 ms 51248 KB Output is correct
5 Correct 708 ms 25908 KB Output is correct
6 Correct 939 ms 51096 KB Output is correct
7 Correct 650 ms 34968 KB Output is correct
8 Correct 709 ms 51188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 336 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Runtime error 1 ms 336 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Runtime error 1 ms 336 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 132 ms 40728 KB Output is correct
4 Correct 882 ms 51248 KB Output is correct
5 Correct 708 ms 25908 KB Output is correct
6 Correct 939 ms 51096 KB Output is correct
7 Correct 650 ms 34968 KB Output is correct
8 Correct 709 ms 51188 KB Output is correct
9 Runtime error 1 ms 336 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -