제출 #743591

#제출 시각아이디문제언어결과실행 시간메모리
743591onebit1024Rainforest Jumps (APIO21_jumps)C++17
0 / 100
4011 ms25292 KiB
#include "jumps.h"

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"

const double PI=3.141592653589;


void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");} 

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif


vector<vector<int>>up;
vector<int>bef,aft,a;
int n;
void init(int N, std::vector<int> H) {
  n = N;
  bef.resize(n+1,-1);
  aft.resize(n+1,-1);
  up = vector<vector<int>>(n+1, vector<int>(21));
  a = {0};
  for(auto x : H)a.pb(x);
  stack<int>st;
  for(int i = 1;i<=N;++i){
    while(!st.empty() && a[i] > a[st.top()])st.pop();
    if(!st.empty())bef[i] = st.top();
    st.push(i);
  }
  while(!st.empty())st.pop();
  for(int i = N;i>=1;--i){
    while(!st.empty() && a[i] > a[st.top()])st.pop();
    if(!st.empty()){
      up[i][0] = st.top();
      aft[i] = st.top();
      for(int j = 1;j<=20;++j)up[i][j] = up[up[i][j-1]][j-1];
    }
    st.push(i);
  }
}

int mn(int u, int v){
  // min cost from u->v
  int res = 0;
  if(u==v)return 0;
  for(int j = 20;j>=0;--j){
    if(up[u][j] <= v && up[u][j]!=0)u = up[u][j], res+=(1ll<<j);
  }
  if(u==v)return res;
  return 1e9;
}

int minimum_jumps(int A, int B, int C, int D) {
  int res = mn(A+1,D+1);
  int u = A+1;
  D++;
  int tot = 0;
  while(1){
    if(u==D)break;
    if(aft[u]==-1 && bef[u]==-1){
      res = min(res, mn(u,D));
      break;
    }
    pair<int,int>best= {-1e9,0};
    if(aft[u]!=-1)best = max(best, {a[aft[u]],1});
    if(bef[u]!=-1)best = max(best, {a[bef[u]],0});

    if(best.second==0 && a[bef[u]]<a[D])u = bef[u];
    else u = aft[u];
    tot++;

    res = min(res,mn(u,D)+tot);
  }
  if(res>=1e9)res = -1;
  return 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...