Submission #426110

#TimeUsernameProblemLanguageResultExecution timeMemory
426110ocarimaRainforest Jumps (APIO21_jumps)C++14
0 / 100
266 ms48452 KiB
#include "jumps.h" #include <vector> #include<bits/stdc++.h> using namespace std; #define lli long long #define rep(i, a, b) for(lli i = (a); i <= (b); ++i) #define repa(i, a, b) for(lli i = (a); i >= (b); --i) #define nl "\n" #define debugsl(x) cout << #x << " = " << x << ", " #define debug(x) debugsl(x) << nl #define debugarr(x, a, b) cout << #x << " = ["; rep(ii, a, b){ cout << x[ii] << ", "; } cout << "]" << nl #define MAX_N 200003 #define LOG_N 17 int maximo[LOG_N][MAX_N], minimo[LOG_N][MAX_N], n, pot[MAX_N]; int izq[MAX_N], der[MAX_N]; int k, res; int maxrango(int l, int r){ k = pot[r - l + 1]; return max(maximo[k][l], maximo[k][r - (1 << k) + 1]); } int minrango(int l, int r){ k = pot[r - l + 1]; return min(minimo[k][l], minimo[k][r - (1 << k) + 1]); } void init(int N, std::vector<int> H) { int p = 1, k = 0; n = N; rep(i, 0, n - 1) maximo[0][i + 1] = minimo[0][i + 1] = H[i]; maximo[0][0] = maximo[0][n + 1] = minimo[0][0] = minimo[0][n + 1] = n + 1; rep(k, 1, LOG_N - 1){ rep(i, 0, (n + 1) - p - p + 1){ maximo[k][i] = max(maximo[k - 1][i], maximo[k - 1][i + p]); minimo[k][i] = min(minimo[k - 1][i], minimo[k - 1][i + p]); } p <<= 1; } p = 1; rep(i, 1, MAX_N - 1){ pot[i] = k; if (p + p <= i){ p <<= 1; ++k; } } // OBTEN EL SIGUIENTE SALTO A LA DERECHA Y A LA IZQUIERDA DE CADA NUMERO int l, r, mitad, cur; rep(i, 1, n){ // OBTEN SU SALTO A LA IZQUIERDA; l = 0; r = i - 1; cur = l; while (l <= r){ mitad = (l + r) / 2; if (maxrango(mitad, i - 1) > H[i - 1]){ l = mitad + 1; cur = mitad; } else r = mitad - 1; } izq[i] = cur; // OBTEN SU SALTO A LA DERECHA; l = i + 1; r = n + 1; cur = n + 1; while (l <= r){ mitad = (l + r) / 2; if (maxrango(i + 1, mitad) > H[i - 1]){ r = mitad - 1; cur = mitad; } else l = mitad + 1; } der[i] = cur; } } int minimum_jumps(int A, int B, int C, int D) { res = n + 1; ++A; ++B; ++C; ++D; int maxdestino; int cnt, pos; maxdestino = (maxrango(C, D)); rep(i, A, B){ pos = i; cnt = 0; while (pos != 0 && pos != n + 1){ if (der[pos] >= C && der[pos] <= D) pos = der[pos]; else if (izq[pos] < maxdestino && izq[pos] > der[pos]) pos = izq[pos]; else if (der[pos] < maxdestino && der[pos] > izq[pos]) pos = der[pos]; else{ cnt = n + 1; break; } ++cnt; if (pos >= C && pos <= D){ res = min(res, cnt); break; } } } return (res == n + 1) ? -1 : res; }
#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...