#include <bits/stdc++.h>
//#include "jumps.h"
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
#define sz(x) (int)x.size()
#define ff first
#define se second
#define mp make_pair
using ll = long long;
int mod = (ll)1e9 + 7;
const int INF = 1e9 + 1;
const double eps = 1e-7;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template<class T, size_t SZ> using AR = array<T, SZ>;
template<class T> using PR = pair<T, T>;
template <typename XPAX>
bool ckma(XPAX &x, XPAX y) {
return (x < y ? x = y, 1 : 0);
}
template <typename XPAX>
bool ckmi(XPAX &x, XPAX y) {
return (x > y ? x = y, 1 : 0);
}
const int mxn = 2e5+2;
const int G = 20;
int F[mxn][G], L[mxn][G], R[mxn][G], hi[mxn][G];
V<int> H;
int n;
void init(int N, V<int> a) {
H = a;
H.insert(H.begin(), INF);
H.push_back(INF);
n = N;
H[N] = N;
stack<int> st;
forn(i, N+2) {
while(sz(st) && H[st.top()] <= H[i])
st.pop();
L[i][0] = sz(st) ? st.top() : i;
st.push(i);
}
while(sz(st))st.pop();
rforn(i, N+2) {
while(sz(st) && H[st.top()] <= H[i])
st.pop();
R[i][0] = sz(st) ? st.top() : i;
st.push(i);
}
forn(i,n+2) F[i][0] = (H[L[i][0]] > H[R[i][0]] ? L[i][0] : R[i][0]);
for(int j = 1;j < G;++j) forn(i,n+2) {
L[i][j] = L[L[i][j-1]][j-1];
R[i][j] = R[R[i][j-1]][j-1];
F[i][j] = F[F[i][j-1]][j-1];
}
}
int minimum_jumps(int A, int B, int C, int D) {
++A, ++B, ++C, ++D;
if(B + 1 == C) {
return R[B][0] <= D ? 1 : -1;
}
int md = B + 1;
// md is biggest element in (B,C)
// we have to cross through md eventually
rforn(i, G) if(R[md][i] < C)md = R[md][i];
if(H[B] > H[md])
return R[md][0] <= D ? 1 : -1;
int go = B;
// the optimal is where the H[index] in greatest integer < H[mid] in range [A, B]
// go is where we start from
rforn(i, G) if(L[go][i] >= A && H[L[go][i]] < H[md]) go = L[go][i];
int ret = 0;
if(L[go][0] >= A) {
if(R[L[go][0]][0] <= D)return 1;
}
else {
rforn(i, G) if(H[hi[go][i]] <= H[md]) {
go = hi[go][i];
ret += 1 << i;
}
if(go == md)return ret+1;
else if(L[go][0] && R[L[go][0]][0] <= D)return ret+2;
}
rforn(i, G) if(R[go][i] < C) {
ret += 1 << i;
go = R[go][i];
}
go = R[go][0];
if(C <= go && go <= D)return ret+1;
else return -1;
}
/*
int main() {
init(7, {3, 2, 1, 6, 4, 5, 7});
cout << minimum_jumps(3, 4, 6, 6) << '\n';
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
200 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
171 ms |
40108 KB |
Output is correct |
4 |
Correct |
1158 ms |
50340 KB |
Output is correct |
5 |
Correct |
972 ms |
25532 KB |
Output is correct |
6 |
Correct |
1022 ms |
50316 KB |
Output is correct |
7 |
Correct |
647 ms |
34484 KB |
Output is correct |
8 |
Correct |
666 ms |
50440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
200 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
0 ms |
200 KB |
Output is correct |
4 |
Correct |
0 ms |
200 KB |
Output is correct |
5 |
Incorrect |
2 ms |
328 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
200 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
0 ms |
200 KB |
Output is correct |
4 |
Correct |
0 ms |
200 KB |
Output is correct |
5 |
Incorrect |
2 ms |
328 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
200 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
0 ms |
200 KB |
Output is correct |
4 |
Correct |
0 ms |
200 KB |
Output is correct |
5 |
Correct |
92 ms |
39988 KB |
Output is correct |
6 |
Incorrect |
116 ms |
49584 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
200 KB |
Output is correct |
3 |
Correct |
0 ms |
200 KB |
Output is correct |
4 |
Incorrect |
280 ms |
22840 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
200 KB |
Output is correct |
3 |
Correct |
0 ms |
200 KB |
Output is correct |
4 |
Incorrect |
280 ms |
22840 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
200 KB |
Output is correct |
2 |
Correct |
0 ms |
200 KB |
Output is correct |
3 |
Correct |
171 ms |
40108 KB |
Output is correct |
4 |
Correct |
1158 ms |
50340 KB |
Output is correct |
5 |
Correct |
972 ms |
25532 KB |
Output is correct |
6 |
Correct |
1022 ms |
50316 KB |
Output is correct |
7 |
Correct |
647 ms |
34484 KB |
Output is correct |
8 |
Correct |
666 ms |
50440 KB |
Output is correct |
9 |
Correct |
1 ms |
200 KB |
Output is correct |
10 |
Correct |
0 ms |
200 KB |
Output is correct |
11 |
Correct |
0 ms |
200 KB |
Output is correct |
12 |
Correct |
0 ms |
200 KB |
Output is correct |
13 |
Incorrect |
2 ms |
328 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |