제출 #552434

#제출 시각아이디문제언어결과실행 시간메모리
552434cig32밀림 점프 (APIO21_jumps)C++17
60 / 100
1037 ms34964 KiB
#include "jumps.h"

#include <stack>
#include <vector>
#include <iostream>
#include <queue>

const int MAXN = 200010;

int L[MAXN], R[MAXN];
int N_;
int mi[19][MAXN], ma[19][MAXN];
bool st1;
int V[MAXN];

void init(int N, std::vector<int> H) {
  N_ = N;
  st1 = 1;
  for(int i=0; i<N; i++) {
    L[i] = R[i] = -1;
    V[i] = H[i];
    if(H[i] != i + 1) st1 = 0;
  }
  std::stack<int> st;
  for(int i=0; i<N; i++) {
    while(st.size() && H[st.top()] < H[i]) {
      st.pop();
    }
    if(st.size()) L[i] = st.top();
    st.push(i);
  }
  while(st.size()) st.pop();
  for(int i=N-1; i>=0; i--) {
    while(st.size() && H[st.top()] < H[i]) {
      st.pop();
    }
    if(st.size()) R[i] = st.top();
    st.push(i);
  }
  while(st.size()) st.pop();
  for(int i=0; i<N; i++) {
    if(L[i] == -1 && R[i] == -1) {
      mi[0][i] = ma[0][i] = -1;
    }
    else if(L[i] == -1) {
      mi[0][i] = ma[0][i] = R[i];
    }
    else if(R[i] == -1) {
      mi[0][i] = ma[0][i] = L[i];
    }
    else {
      mi[0][i] = (H[L[i]] < H[R[i]] ? L[i] : R[i]);
      ma[0][i] = (H[L[i]] > H[R[i]] ? L[i] : R[i]);
    }
  }
  for(int i=1; i<19; i++) {
    for(int j=0; j<N; j++) {
      if(mi[i-1][j] == -1) mi[i][j] = -1;
      else mi[i][j] = mi[i-1][mi[i-1][j]];
      if(ma[i-1][j] == -1) ma[i][j] = -1;
      else ma[i][j] = ma[i-1][ma[i-1][j]];
    }
  }
  
}
int qcnt = 0;
int minimum_jumps(int A, int B, int C, int D) {
  if(st1) return C - B;
  int N = N_;
  qcnt++;
  if(N <= 2000 || qcnt <= 5) {
    int ans = 1e9;
    int dist[N];
    bool vis[N];
    for(int i=0; i<N; i++) dist[i] = 1e9, vis[i] = 0;
    for(int i=A; i<=B; i++) dist[i] = 0;
    std::queue<int> q;
    for(int i=A; i<=B; i++) q.push(i);
    while(q.size()) {
      int f = q.front();
      q.pop();
      if(vis[f]) continue;
      vis[f] = 1;
      if(L[f] != -1 && !vis[L[f]] && dist[L[f]] > dist[f] + 1) {
        dist[L[f]] = dist[f] + 1;
        q.push(L[f]);
      }
      if(R[f] != -1 && !vis[R[f]] && dist[R[f]] > dist[f] + 1) {
        dist[R[f]] = dist[f] + 1;
        q.push(R[f]);
      }
    }
    for(int i=C; i<=D; i++) ans = std::min(ans, dist[i]);
    return (ans == 1e9 ? -1 : ans);
  }
  int ans = 0;
  int mm = 0, id;
  for(int i=A; i<=B; i++) {
    if(V[i] < V[C] && mm < V[i]) {
      mm = V[i], id = i;
    }
  }
  if(mm == 0) return -1;
  A = id;
  for(int i=18; i>=0; i--) {
    if(ma[i][A] != -1) {
      if(V[ma[i][A]] <= V[C]) {
        A = ma[i][A];
        ans += (1 << i);
      }
    }
  }
  for(int i=18; i>=0; i--) {
    if(mi[i][A] != -1) {
      if(V[mi[i][A]] <= V[C]) {
        A = mi[i][A];
        ans += (1 << i);
      }
    }
  }
  return (A == C ? ans : -1);
}
/*
Compilation: 

./compile_sh
./jumps
*/
#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...