#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> h;
vector<int> minV;
vector<int> maxV;
vector<vector<int>> liftingMin;
vector<vector<int>> liftingMax;
void init(int N, std::vector<int> H) {
n = N, h = H;
minV = maxV = vector<int>(n);
liftingMin = liftingMax = vector<vector<int>>(n, vector<int>(30, -1));
{
stack<pair<int, int>> st;
st.push({1e9, -1});
for (int i = 0; i < n; i++) {
while (st.top().first < h[i]) st.pop();
minV[i] = st.top().second;
st.push({h[i], i});
}
}
{
stack<pair<int, int>> st;
st.push({1e9, -1});
for (int i = n-1; i >= 0; i--) {
while (st.top().first < h[i]) st.pop();
maxV[i] = st.top().second;
st.push({h[i], i});
}
}
//for (int x : minV) cout << x << " "; cout << endl;
//for (int x : maxV) cout << x << " "; cout << endl;
//return;
for (int i = 0; i < n; i++) {
if (minV[i] == -1 && maxV[i] == -1) continue;
if (minV[i] == -1) {
minV[i] = maxV[i];
} else if (maxV[i] == -1) {
maxV[i] = minV[i];
} else if (h[minV[i]] > h[maxV[i]]) swap(minV[i], maxV[i]);
}
//for (int x : minV) cout << x << " "; cout << endl;
//for (int x : maxV) cout << x << " "; cout << endl;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) v.push_back({h[i], i});
sort(v.rbegin(), v.rend());
for (int j = 1; j < n; j++) {
int i = v[j].second;
liftingMin[i][0] = minV[i];
liftingMax[i][0] = maxV[i];
for (int j = 1; j < 30; j++) {
if (liftingMin[i][j-1] != -1) liftingMin[i][j] = liftingMin[liftingMin[i][j-1]][j-1];
if (liftingMax[i][j-1] != -1) liftingMax[i][j] = liftingMax[liftingMax[i][j-1]][j-1];
}
}
//for (int i = 0; i < n; i++) {
//for (int j = 0; j < 5; j++) cout << liftingMax[i][j] << " ";
//cout << endl;
//}
}
pair<int, int> blift(vector<vector<int>>& g, int u, int v) {
for (int i = -1; i < 30; i++) {
if (g[u][i + 1] == -1 || h[g[u][i + 1]] > h[v]) {
if (i == -1) return {0, u};
else {
//cout << "END AT " << g[u][i] << endl;
pair<int, int> p = blift(g, g[u][i], v);
return {(1<<i) + p.first, p.second};
}
}
}
}
int minimum_jumps(int A, int B, int C, int D) {
if (A == B && C == D) {
pair<int, int> p = blift(liftingMax, A, D);
//cout << p.first << " " << p.second << endl;
pair<int, int> q = blift(liftingMin, p.second, D);
if (h[q.second] != h[D]) return -1;
else return p.first + q.first;
}
}
//7 0
//3 2 1 6 4 5 7
int main2() {
int N, Q;
//assert(2 == scanf("%d %d", &N, &Q));
std::vector<int> H(N);
N = 12;
Q = 10000;
H = vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
//H = vector<int>{3, 2, 1, 6, 4, 5, 7, 8, 10, 12, 11, 9};
for (int i = 0; i < N; ++i) {
//assert(1 == scanf("%d", &H[i]));
}
init(N, H);
for (int i = 0; i < Q; ++i) {
int A, B, C, D;
assert(4 == scanf("%d %d %d %d", &A, &B, &C, &D));
printf("%d\n", minimum_jumps(A, B, C, D));
}
return 0;
}
Compilation message
jumps.cpp: In function 'std::pair<int, int> blift(std::vector<std::vector<int> >&, int, int)':
jumps.cpp:80:1: warning: control reaches end of non-void function [-Wreturn-type]
80 | }
| ^
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:90:1: warning: control reaches end of non-void function [-Wreturn-type]
90 | }
| ^
jumps.cpp: In function 'int main2()':
jumps.cpp:98:23: warning: 'N' is used uninitialized in this function [-Wuninitialized]
98 | std::vector<int> H(N);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Incorrect |
257 ms |
52932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
310 ms |
30440 KB |
Output is correct |
5 |
Correct |
984 ms |
65832 KB |
Output is correct |
6 |
Correct |
672 ms |
11424 KB |
Output is correct |
7 |
Correct |
1018 ms |
65884 KB |
Output is correct |
8 |
Correct |
590 ms |
23232 KB |
Output is correct |
9 |
Correct |
1081 ms |
65880 KB |
Output is correct |
10 |
Correct |
1100 ms |
65884 KB |
Output is correct |
11 |
Correct |
1050 ms |
65840 KB |
Output is correct |
12 |
Correct |
1212 ms |
65876 KB |
Output is correct |
13 |
Correct |
967 ms |
65884 KB |
Output is correct |
14 |
Correct |
1182 ms |
66472 KB |
Output is correct |
15 |
Correct |
1002 ms |
67400 KB |
Output is correct |
16 |
Correct |
972 ms |
67372 KB |
Output is correct |
17 |
Correct |
0 ms |
208 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
208 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
11 ms |
848 KB |
Output is correct |
28 |
Correct |
19 ms |
848 KB |
Output is correct |
29 |
Correct |
17 ms |
848 KB |
Output is correct |
30 |
Correct |
20 ms |
848 KB |
Output is correct |
31 |
Correct |
14 ms |
848 KB |
Output is correct |
32 |
Correct |
0 ms |
208 KB |
Output is correct |
33 |
Correct |
124 ms |
38092 KB |
Output is correct |
34 |
Correct |
229 ms |
65836 KB |
Output is correct |
35 |
Correct |
190 ms |
65848 KB |
Output is correct |
36 |
Correct |
187 ms |
65876 KB |
Output is correct |
37 |
Correct |
220 ms |
66572 KB |
Output is correct |
38 |
Correct |
153 ms |
67324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Correct |
310 ms |
30440 KB |
Output is correct |
5 |
Correct |
984 ms |
65832 KB |
Output is correct |
6 |
Correct |
672 ms |
11424 KB |
Output is correct |
7 |
Correct |
1018 ms |
65884 KB |
Output is correct |
8 |
Correct |
590 ms |
23232 KB |
Output is correct |
9 |
Correct |
1081 ms |
65880 KB |
Output is correct |
10 |
Correct |
1100 ms |
65884 KB |
Output is correct |
11 |
Correct |
1050 ms |
65840 KB |
Output is correct |
12 |
Correct |
1212 ms |
65876 KB |
Output is correct |
13 |
Correct |
967 ms |
65884 KB |
Output is correct |
14 |
Correct |
1182 ms |
66472 KB |
Output is correct |
15 |
Correct |
1002 ms |
67400 KB |
Output is correct |
16 |
Correct |
972 ms |
67372 KB |
Output is correct |
17 |
Correct |
0 ms |
208 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
2 ms |
208 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
11 ms |
848 KB |
Output is correct |
28 |
Correct |
19 ms |
848 KB |
Output is correct |
29 |
Correct |
17 ms |
848 KB |
Output is correct |
30 |
Correct |
20 ms |
848 KB |
Output is correct |
31 |
Correct |
14 ms |
848 KB |
Output is correct |
32 |
Correct |
0 ms |
208 KB |
Output is correct |
33 |
Correct |
124 ms |
38092 KB |
Output is correct |
34 |
Correct |
229 ms |
65836 KB |
Output is correct |
35 |
Correct |
190 ms |
65848 KB |
Output is correct |
36 |
Correct |
187 ms |
65876 KB |
Output is correct |
37 |
Correct |
220 ms |
66572 KB |
Output is correct |
38 |
Correct |
153 ms |
67324 KB |
Output is correct |
39 |
Correct |
0 ms |
208 KB |
Output is correct |
40 |
Correct |
0 ms |
208 KB |
Output is correct |
41 |
Correct |
0 ms |
208 KB |
Output is correct |
42 |
Correct |
273 ms |
30440 KB |
Output is correct |
43 |
Correct |
942 ms |
65840 KB |
Output is correct |
44 |
Correct |
575 ms |
11332 KB |
Output is correct |
45 |
Correct |
1117 ms |
65812 KB |
Output is correct |
46 |
Correct |
632 ms |
23184 KB |
Output is correct |
47 |
Correct |
993 ms |
65816 KB |
Output is correct |
48 |
Correct |
1266 ms |
65856 KB |
Output is correct |
49 |
Correct |
1134 ms |
66024 KB |
Output is correct |
50 |
Correct |
1026 ms |
65796 KB |
Output is correct |
51 |
Correct |
1044 ms |
65860 KB |
Output is correct |
52 |
Correct |
1149 ms |
66500 KB |
Output is correct |
53 |
Correct |
1122 ms |
67356 KB |
Output is correct |
54 |
Correct |
816 ms |
67352 KB |
Output is correct |
55 |
Correct |
0 ms |
208 KB |
Output is correct |
56 |
Incorrect |
283 ms |
65552 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Incorrect |
257 ms |
52932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |