Submission #971378

#TimeUsernameProblemLanguageResultExecution timeMemory
971378AlperenT_Rainforest Jumps (APIO21_jumps)C++17
8 / 100
4086 ms54532 KiB
#include "jumps.h" #include <vector> #include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") #define pb push_back #define F first #define S second #define ld long double #define all(a) a.begin(),a.end() #define pii pair <int,int> #define PII pair<pii , pii> #define sz(v) (int)v.size() #define rep(i , a , b) for(int i=a;i <= (b);i++) #define per(i , a , b) for(int i=a;i >= (b);i--) #define deb(x) cout <<#x << " : " << x << "\n" ; using namespace std ; const int maxn = 2e5 + 10 , maxq = 32, inf = 1e9+10 , lg = 19 ,sq = 707 ,mod = 998244353 ; int mx[maxn][lg+1] , c = 1 , L[maxn] , R[maxn], h[maxn] , dis[maxn] , par[maxn][lg+1] , par2[maxn ]; vector< int> G[maxn] ; void dfs(int v){ for(int u : G[v]){ dis[u] = dis[v] + 1; dfs(u) ; } } void init(int n, vector<int> H) { rep(i ,1 ,n){ h[i] = H[i-1] ; mx[i][0] = i ; } vector <int> vec;; rep(i , 1, n){ while(sz(vec) && h[vec.back()] < h[i])vec.pop_back() ; L[i] = -1 ; if(sz(vec)!=0)L[i] = vec.back() ; vec.pb(i) ; } vec.clear() ; per(i ,n,1){ while(sz(vec) && h[vec.back()] < h[i])vec.pop_back() ; R[i] = -1 ; if(sz(vec)!=0)R[i] = vec.back() ; vec.pb(i) ; } int id =0 ; rep(i ,1 ,n){ if(L[i]+R[i] == -2){ id = i ; continue ; } if(L[i]!=-1 && (R[i] == -1 || h[L[i]] < h[R[i]])){ G[L[i]].pb(i) ;par2[i] = L[i] ; if(R[i] == -1)par[i][0] = L[i] ; else par[i][0] = R[i] ; }else{ G[R[i]].pb(i) ;par2[i] = R[i] ; if(L[i] == -1)par[i][0] = R[i] ; else par[i][0] = L[i] ; } } dfs(id) ; rep(i ,1 ,n){ rep(j , 1 ,lg){ par[i][j] = par[par[i][j-1]][j-1]; } } } int answer(int a, int b){ if(L[b] >= a)return inf ; int ans =0 ; /* per(i , lg , 0){ if(par[a][i] != 0 && dis[par[a][i]] >= dis[b]){ a = par[a][i] ; ans += (1<<i); } } */ while(a!=b && h[par[a][0]] <= h[b]){ ans ++ ; a= par[a][0] ; } while(a != b){ ans ++ ; a = par2[a] ; } return ans; } int minimum_jumps(int A, int B, int C, int D) { A++;B++;C++;D++; int ans = inf ; rep(i , A ,B){ rep(j, C , D){ ans = min(ans , answer(i , j)) ; } } return (ans == inf ? -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...