Submission #1052574

#TimeUsernameProblemLanguageResultExecution timeMemory
1052574kachim2Rainforest Jumps (APIO21_jumps)C++17
60 / 100
4034 ms45788 KiB
#include "jumps.h"
//#include "stub.cpp"
#include<queue>
#include<array>
#include <vector>
#include <iostream>
using namespace std;
std::vector<int> H;
vector<int> nxt;
vector<int> prv;
vector<int> d;
vector<array<int, 25>> jp;
vector<array<int, 25>> njp;
bool subtask1=1;

void init(int N, std::vector<int> nH)
{
  for(int i = 0; i < N; i++){
    if(nH[i]!=i+1) subtask1=0;
  }
  H = nH;
  
  nxt.resize(N);
  prv.resize(N);
  jp.resize(N);
  njp.resize(N);
  {
  vector<int> kmon;
  for (int i = 0; i < N; i++)
  {
    if (kmon.empty())
    {
      kmon.push_back(i);
      prv[i] = -1;
      continue;
    }
    while (H[kmon.back()] < H[i])
    {
      kmon.pop_back();
      if (kmon.empty())
      {
        kmon.push_back(i);
        prv[i] = -1;
        goto nd;
      }
    }
    prv[i] = kmon.back();
    kmon.push_back(i);
    nd:;
  }
  }
  {
  vector<int> kmon;
  for (int i = N-1; i>=0; i--)
  {
    if (kmon.empty())
    {
      kmon.push_back(i);
      nxt[i] = -1;
      continue;
    }
    while (H[kmon.back()] < H[i])
    {
      kmon.pop_back();
      if (kmon.empty())
      {
        kmon.push_back(i);
        nxt[i] = -1;
        goto nd2;
      }
    }
    nxt[i] = kmon.back();
    kmon.push_back(i);
    nd2:;
  }
  }
  
  for(int i = 0; i < N; i++){
    if(H[prv[i]]>H[nxt[i]]){
      jp[i][0] = prv[i]; 
    }else if (H[prv[i]]<H[nxt[i]]){
      jp[i][0] = nxt[i];
    }else{
      jp[i][0] = -1;
    }
  }
  for(int j = 1; j < jp[0].size(); j++){
    for(int i = 0; i < N; i++){
      if(jp[i][j-1] != -1) 
      jp[i][j] = jp[jp[i][j-1]][j-1];
      else jp[i][j] = -1;
    }
  }
  for(int i = 0; i < N; i++){
    njp[i][0] = nxt[i];
  }
  for(int j = 1; j < njp[0].size(); j++){
    for(int i = 0; i < N; i++){
      if(njp[i][j-1] != -1) 
      njp[i][j] = njp[njp[i][j-1]][j-1];
      else njp[i][j] = -1;
    }
  }

}

int minimum_jumps(int A, int B, int C, int D)
{
  if (subtask1) return C-B;
if( C==D){
int cur = -1;
for(int i = B; i >= A; i--){
  if(H[i] > H[C]){
    break;
  }
  if(H[i] > H[cur]) cur = i;
}
if(cur == -1) return -1;
int res=0;
for(int i = jp[0].size()-1; i>=0; i--){
  if(jp[cur][i]!=-1){
    if(H[jp[cur][i]] <= H[C]){
      res+=1<<i;
      if(jp[cur][i] == C) return res;
      cur = jp[cur][i];
      
    }
  }
}
for(int i = njp[0].size()-1; i>=0; i--){
  if(njp[cur][i]!=-1){
    if(njp[cur][i] <= C){
      res+=1<<i;
      if(njp[cur][i] == C) return res;
      cur = njp[cur][i];
      
    }
  }
}

return -1;
























}

  int mcd = 0;
  for (int i = C; i <= D; i++)
  {
    mcd = max(mcd, H[i]);
  }
  int bs = 0;
  for (int i = B; i >= A; i--)
  {
    // cerr << H[i] << ' ';
    if (H[i] < mcd)
      bs = max(bs, i);
    else
      break;
  }
  // cerr << bs << '\n';

  vector<bool> visited(H.size(), 0);

  queue<pair<int, int>> q;
  for(int i = A; i <=B; i++)
  q.push({i, 0});
  while(!q.empty()){
    auto x = q.front();
    q.pop();
    if(visited[x.first]) continue;
    visited[x.first] = 1;
    if(x.first >= C && x.first <= D) return x.second;
    if(nxt[x.first]!=-1) q.push({nxt[x.first], x.second+1});
    if(prv[x.first]!=-1) q.push({prv[x.first], x.second+1});
  }
  return -1;
}

Compilation message (stderr)

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::array<int, 25>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |   for(int j = 1; j < jp[0].size(); j++){
      |                  ~~^~~~~~~~~~~~~~
jumps.cpp:97:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::array<int, 25>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   for(int j = 1; j < njp[0].size(); j++){
      |                  ~~^~~~~~~~~~~~~~~
#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...