Submission #489558

#TimeUsernameProblemLanguageResultExecution timeMemory
489558dung11112003Rainforest Jumps (APIO21_jumps)C++11
48 / 100
1525 ms75488 KiB
#include "jumps.h"

#include <bits/stdc++.h>

using namespace std;

int n;
vector <int> a, L, R, nxt;
vector <vector <int>> jumpL, jumpR, jumpbest;

void init(int N, vector<int> H)
{
    n = N;
    a = H;
    for (auto &x: a)
    {
        x--;
    }
    L.assign(n, 0);
    R.assign(n, 0);
    stack <int> st;
    for (int i = 0; i < n; i++)
    {
        while (!st.empty() && a[st.top()] < a[i])
        {
            st.pop();
        }
        L[i] = (st.empty() ? -1 : st.top());
        st.push(i);
    }
    st = stack <int>();
    for (int i = n - 1; i >= 0; i--)
    {
        while (!st.empty() && a[st.top()] < a[i])
        {
            st.pop();
        }
        R[i] = (st.empty() ? n : st.top());
        st.push(i);
    }
    jumpL.assign(n, vector <int> (20));
    for (int i = 0; i < n; i++)
    {
        jumpL[i][0] = L[i];
    }
    for (int j = 1; j < 20; j++)
    {
        for (int i = 0; i < n; i++)
        {
            jumpL[i][j] = (jumpL[i][j - 1] == -1 ? -1 : jumpL[jumpL[i][j - 1]][j - 1]);
        }
    }
    jumpR.assign(n, vector <int> (20));
    for (int i = 0; i < n; i++)
    {
        jumpR[i][0] = R[i];
    }
    for (int j = 1; j < 20; j++)
    {
        for (int i = 0; i < n; i++)
        {
            jumpR[i][j] = (jumpR[i][j - 1] == n ? n : jumpR[jumpR[i][j - 1]][j - 1]);
        }
    }
    jumpbest.assign(n, vector <int> (20));
    for (int i = 0; i < n; i++)
    {
        if (L[i] == -1)
        {
            jumpbest[i][0] = (R[i] == n ? -1 : R[i]);
        }
        else if (R[i] == n)
        {
            jumpbest[i][0] = L[i];
        }
        else
        {
            jumpbest[i][0] = (a[L[i]] < a[R[i]] ? R[i] : L[i]);
        }
    }
    for (int j = 1; j < 20; j++)
    {
        for (int i = 0; i < n; i++)
        {
            jumpbest[i][j] = (jumpbest[i][j - 1] == -1 ? -1 : jumpbest[jumpbest[i][j - 1]][j - 1]);
        }
    }
}

int minimum_jumps(int A, int B, int C, int D)
{
    int cur = B;
    for (int i = 19; i >= 0; i--)
    {
        if (jumpR[cur][i] != -1 && jumpR[cur][i] < C)
        {
            cur = jumpR[cur][i];
        }
    }
    if (R[cur] > D)
    {
        return -1;
    }
    int target = R[cur];
    cur = B;
    for (int i = 19; i >= 0; i--)
    {
        if (jumpL[cur][i] >= A && a[jumpL[cur][i]] < a[target])
        {
            cur = jumpL[cur][i];
        }
    }
    int ans = 0;
    for (int i = 19; i >= 0; i--)
    {
        if (jumpbest[cur][i] != -1 && a[jumpbest[cur][i]] <= a[target])
        {
            cur = jumpbest[cur][i];
            ans += 1 << i;
        }
    }
    for (int i = 19; i >= 0; i--)
    {
        if (jumpR[cur][i] <= target)
        {
            cur = jumpR[cur][i];
            ans += 1 << i;
        }
    }
    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...