#include "jumps.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int B = 20;
int n;
vector<vector<int>> build(vector<vector<int>> g,vector<int>rt){
vector<vector<int>> up(n+1);
function<void(int,int )> dfs = [&](int v,int pr) {
up[v].resize(B);
up[v][0] = pr;
for(int i = 1;i < B;i++){
up[v][i] = up[up[v][i - 1]][i - 1];
}
for(int to:g[v]) {
dfs(to,v);
}
};
for(int i:rt) {
dfs(i,i);
}
return up;
}
vector<vector<int>> up,up1;
const int maxn = (int)1e5 + 12;
int l[maxn],r[maxn];
void init(int N, vector<int> H) {
n = N;
vector<int> st,rt;
vector<vector<int>> G(n);
for(int i = 0;i < N;i++) {
while(!(int)st.empty() && H[st.back()] < H[i]) {
st.pop_back();
}
if(st.empty()){
rt.push_back(i);
l[i] = -1;
}else {
G[st.back()].push_back(i);
l[i] = st.back();
}
st.push_back(i);
}
up = build(G,rt);
for(int i = 0;i < n;i++){
G[i].clear();
}
rt.clear();
st.clear();
for(int i = N - 1;i >= 0;i--){
while(!st.empty() && H[st.back()] < H[i]){
st.pop_back();
}
if(st.empty()){
rt.push_back(i);
r[i] = -1;
}else {
G[st.back()].push_back(i);
r[i] = st.back();
}
st.push_back(i);
}
up1 = build(G,rt);
}
bool check(int ver,int C,int D){
for(int i = B - 1;i >= 0;i--){
if(up1[ver][i] < C){
ver = up1[ver][i];
}
}
ver = up1[ver][0];
return (ver >= C && ver <= D);
}
int go(int ver,int C,int D) {
if(ver >= C && ver <= D) return 0;
if(l[ver] == -1 || r[ver] > l[ver] || !check(l[ver],C,D)){
return go(r[ver],C,D) + 1;
}
return go(l[ver],C,D) + 1;
}
int minimum_jumps(int A, int B, int C, int D) {
int ver = B;
for(int i = B - 1;i >= 0;i--){
if(up[ver][i] >= A && check(ver,C,D) != -1){
ver = up[ver][i];
}
}
if(!check(ver,C,D)){
return -1;
}
return go(ver,C,D);
}
Compilation message
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:86:46: warning: comparison of constant '-1' with boolean expression is always true [-Wbool-compare]
86 | if(up[ver][i] >= A && check(ver,C,D) != -1){
| ~~~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Runtime error |
16 ms |
12344 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Runtime error |
27 ms |
14884 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Runtime error |
72 ms |
57476 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Runtime error |
72 ms |
57476 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Runtime error |
16 ms |
12344 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |