제출 #887784

#제출 시각아이디문제언어결과실행 시간메모리
887784hamidh100밀림 점프 (APIO21_jumps)C++17
33 / 100
4083 ms6128 KiB
#include "jumps.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define YES cout << "YES\n"
#define NO cout << "NO\n"

const ll INF = (ll)2e18 + 1386;
const ld EPS = 0.000000000000001;
const int MOD = 1e9 + 7;

inline int _add(int a, int b){ int res = a + b; return (res >= MOD ? res - MOD : res); }
inline int _neg(int a, int b){ int res = (abs(a - b) < MOD ? a - b : (a - b) % MOD); return (res < 0 ? res + MOD : res); }
inline int _mlt(ll a, ll b){ return (a * b % MOD); }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }

const int MAXN = 2e5 + 5;

int n;
int a[MAXN], dist[MAXN], cand[MAXN][2];
bitset<MAXN> mark;

void init(int N, std::vector<int> H) {
    n = N;
    VI agha;
    for (int i = 1; i <= n; i++){
        a[i] = H[i - 1];
        while (!agha.empty() && a[i] > a[agha.back()]){
            agha.pop_back();
        }
        if (!agha.empty()) cand[i][0] = agha.back();
        agha.PB(i);
    }
    agha.clear();
    for (int i = n; i >= 1; i--){
        while (!agha.empty() && a[i] > a[agha.back()]){
            agha.pop_back();
        }
        if (!agha.empty()) cand[i][1] = agha.back();
        agha.PB(i);
    }
}

int minimum_jumps(int A, int B, int C, int D) {
    int ans = INT_MAX;
    A++, B++, C++, D++;
    mark.reset();
    fill(dist, dist + n + 1, INT_MAX);
    queue<int> q;
    for (int i = A; i <= B; i++){
        q.push(i);
        mark[i] = 1;
        dist[i] = 0;
    }
    while (!q.empty()){
        int v = q.front();
        q.pop();
        for (int i = 0; i < 2; i++){
            if (cand[v][i] != 0 && !mark[cand[v][i]]){
                mark[cand[v][i]] = 1;
                dist[cand[v][i]] = dist[v] + 1;
                q.push(cand[v][i]);
            }
        }
    }
    for (int j = C; j <= D; j++){
        ans = min(ans, dist[j]);
    }
    return ans == INT_MAX ? -1 : 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...