#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 res = 1e9;
for(int i = A;i <= B;i++) {
if(check(i,C,D)){
res = min(res,go(i,C,D));
}
}
// int ver = B;
// for(int i = 19;i >= 0;i--){
// if(up[ver][i] >= A && check(ver,C,D) != -1){
// ver = up[ver][i];
// }
// }
if(res == 1e9) res = -1;
return res;
// if(!check(ver,C,D)){
// return -1;
// }
// return go(ver,C,D);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Runtime error |
17 ms |
12324 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
31 ms |
14784 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
198 ms |
30912 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
198 ms |
30912 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Runtime error |
17 ms |
12324 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |