답안 #717806

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
717806 2023-04-02T14:40:49 Z InternetPerson10 밀림 점프 (APIO21_jumps) C++17
56 / 100
1546 ms 75828 KB
#include "jumps.h"
#include <bits/stdc++.h>
 
using namespace std;
 
int subtask = 1;
 
int n;
vector<int> nums, l, r;
vector<vector<int>> liftBig, liftRight, liftLeft;
 
bool invalid(int k) {
    if(k == -1 || k == n) return true;
    return false;
}
 
int bigger(int a, int b) {
    if(invalid(a) && invalid(b)) return -1;
    if(invalid(a)) return b;
    if(invalid(b)) return a;
    if(nums[a] > nums[b]) return a;
    return b;
}
 
void init(int N, std::vector<int> H) {
    bool subtask1 = true;
    for(int i = 0; i < N; i++) {
        if(H[i] != i+1) subtask1 = false;
        H[i]--;
    }
    if(subtask1) subtask = 1;
    else if(N <= 200) subtask = 2;
    else subtask = 3;
    n = N;
    nums = H;
    l.resize(n, -1);
    r.resize(n, n);
    liftBig.resize(n, vector<int>(20));
    liftRight.resize(n, vector<int>(20));
    liftLeft.resize(n, vector<int>(20));
    vector<int> v;
    v.push_back(0);
    for(int i = 1; i < n; i++) {
        while(v.size()) {
            if(nums[v.back()] < nums[i]) {
                r[v.back()] = i;
                v.pop_back();
            }
            else break;
        }
        v.push_back(i);
    }
    v.clear();
    v.push_back(n-1);
    for(int i = n-2; i >= 0; i--) {
        while(v.size()) {
            if(nums[v.back()] < nums[i]) {
                l[v.back()] = i;
                v.pop_back();
            }
            else break;
        }
        v.push_back(i);
    }
    for(int j = 0; j < N; j++) {
        liftBig[j][0] = bigger(l[j], r[j]);
        liftLeft[j][0] = l[j];
        if(r[j] == n) liftRight[j][0] = -1;
        else liftRight[j][0] = r[j];
    }
    for(int i = 1; i < 20; i++) {
        for(int j = 0; j < N; j++) {
            if(liftBig[j][i-1] == -1) liftBig[j][i] = -1;
            else liftBig[j][i] = liftBig[liftBig[j][i-1]][i-1];
            if(liftRight[j][i-1] == -1) liftRight[j][i] = -1;
            else liftRight[j][i] = liftRight[liftRight[j][i-1]][i-1];
            if(liftLeft[j][i-1] == -1) liftLeft[j][i] = -1;
            else liftLeft[j][i] = liftLeft[liftLeft[j][i-1]][i-1];
        }
    }
}
 
int BIG = 1e9 + 7;
 
int reachable(int x, int C, int D) { // With a series of right jumps
    if(C <= x && x <= D) return 0;
    if(x > D) return -1;
    int ans2 = BIG, tot = 0;
    for(int i = 19; i >= 0; i--) {
        if(liftRight[x][i] == -1) continue;
        if(liftRight[x][i] > D) continue;
        if(liftRight[x][i] >= C) ans2 = min(ans2, tot + (1 << i));
        else {
            x = liftRight[x][i];
            tot += (1 << i);
        }
    }
    if(ans2 == BIG) return -1;
    return ans2;
}
 
int single_min_jumps(int x, int C, int D) {
    int r = reachable(x, C, D);
    if(r == -1) return BIG;
    int ans = r;
    int tot = 0;
    if(subtask == 2) {
        while(liftBig[x][0] != -1) {
            x = liftBig[x][0];
            tot++;
            r = reachable(x, C, D);
            if(r != -1) ans = min(ans, r + tot);
        }
        return ans;
    }
    for(int i = 19; i >= 0; i--) {
        if(liftBig[x][i] == -1) continue;
        int r = reachable(liftBig[x][i], C, D);
        if(r == -1) continue;
        ans = min(ans, tot + (1 << i) + r);
        if(r != 0) {
            x = liftBig[x][i];
            tot += (1 << i);
        }
    }
    return ans;
}
 
map<pair<int, pair<int, int>>, int> dp;
 
int single_min_jumps_brute(int x, int C, int D) {
    if(invalid(x)) return BIG;
    pair<int, pair<int, int>> p = {x, {C, D}};
    if(dp.count(p)) return dp[p];
    if(C <= x && x <= D) return dp[p] = 0;
    return dp[p] = min(single_min_jumps_brute(l[x], C, D),
            single_min_jumps_brute(r[x], C, D)) + 1;
}
 
void debug(int A, int B, int C, int D, int ans) {
    int ans2 = BIG;
    for(int i = A; i <= B; i++) {
        ans2 = min(ans2, single_min_jumps_brute(i, C, D));
    }
    if(ans2 == BIG) ans2 = -1;
    if(ans != ans2) {
        cerr << "NUMBERS:\n";
        for(int i = 0; i < n; i++) cerr << i << ' ' << nums[i] << '\n';
        cerr << '\n';
        cerr << "CASE: " << A << ' ' << B << ' ' << C << ' ' << D << '\n';
        cerr << "ANS: " << ans << '\n';
        cerr << "BRUTE: " << ans2 << '\n';
        assert(false);
    }
}
 
int minimum_jumps(int A, int B, int C, int D) {
    if(subtask == 1) {
        return C - B;
    }
    int ans = BIG;
    if(C != D) {
        int X = C;
        for(int i = 19; i >= 0; i--) {
            if(liftRight[X][i] == -1) continue;
            if(liftRight[X][i] > D) continue;
            X = liftRight[X][i];
        }
        for(int i = 19; i >= 0; i--) {
            if(liftLeft[B][i] == -1) continue;
            if(liftLeft[B][i] < A) continue;
            if(reachable(liftLeft[B][i], C, D) == -1) continue;
            B = liftLeft[B][i];
        }
        A = B;
        for(int i = A; i <= B; i++) {
            ans = min(ans, single_min_jumps(i, C, D));
        }
    }
    else {
        for(int i = 19; i >= 0; i--) {
            if(liftLeft[B][i] == -1) continue;
            if(liftLeft[B][i] < A) continue;
            if(reachable(liftLeft[B][i], C, D) == -1) continue;
            B = liftLeft[B][i];
        }
        ans = single_min_jumps(B, C, D);
    }
    if(ans == BIG) ans = -1;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 222 ms 60564 KB Output is correct
4 Correct 1097 ms 75804 KB Output is correct
5 Correct 993 ms 38364 KB Output is correct
6 Correct 1134 ms 75780 KB Output is correct
7 Correct 884 ms 52148 KB Output is correct
8 Correct 1062 ms 75768 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 208 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 3 ms 336 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 3 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 3 ms 336 KB Output is correct
13 Correct 3 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 4 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 208 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 3 ms 336 KB Output is correct
23 Correct 4 ms 336 KB Output is correct
24 Correct 4 ms 336 KB Output is correct
25 Correct 1 ms 208 KB Output is correct
26 Correct 0 ms 296 KB Output is correct
27 Correct 2 ms 208 KB Output is correct
28 Correct 3 ms 336 KB Output is correct
29 Correct 4 ms 336 KB Output is correct
30 Correct 3 ms 336 KB Output is correct
31 Correct 3 ms 336 KB Output is correct
32 Correct 3 ms 336 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 208 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 3 ms 336 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 3 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 3 ms 336 KB Output is correct
13 Correct 3 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 2 ms 336 KB Output is correct
17 Correct 4 ms 336 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 208 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 3 ms 336 KB Output is correct
23 Correct 4 ms 336 KB Output is correct
24 Correct 4 ms 336 KB Output is correct
25 Correct 1 ms 208 KB Output is correct
26 Correct 0 ms 296 KB Output is correct
27 Correct 2 ms 208 KB Output is correct
28 Correct 3 ms 336 KB Output is correct
29 Correct 4 ms 336 KB Output is correct
30 Correct 3 ms 336 KB Output is correct
31 Correct 3 ms 336 KB Output is correct
32 Correct 3 ms 336 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 208 KB Output is correct
41 Correct 1 ms 208 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
43 Correct 0 ms 208 KB Output is correct
44 Correct 1 ms 208 KB Output is correct
45 Correct 3 ms 336 KB Output is correct
46 Correct 3 ms 336 KB Output is correct
47 Correct 4 ms 336 KB Output is correct
48 Correct 2 ms 336 KB Output is correct
49 Correct 3 ms 336 KB Output is correct
50 Correct 3 ms 336 KB Output is correct
51 Correct 3 ms 336 KB Output is correct
52 Correct 2 ms 336 KB Output is correct
53 Correct 4 ms 336 KB Output is correct
54 Correct 3 ms 336 KB Output is correct
55 Correct 3 ms 336 KB Output is correct
56 Correct 3 ms 336 KB Output is correct
57 Correct 1 ms 336 KB Output is correct
58 Correct 15 ms 848 KB Output is correct
59 Incorrect 20 ms 976 KB Output isn't correct
60 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 150 ms 60124 KB Output is correct
6 Correct 165 ms 74664 KB Output is correct
7 Correct 87 ms 38344 KB Output is correct
8 Correct 177 ms 74668 KB Output is correct
9 Correct 24 ms 11412 KB Output is correct
10 Correct 165 ms 74672 KB Output is correct
11 Correct 166 ms 75724 KB Output is correct
12 Correct 164 ms 75720 KB Output is correct
13 Correct 172 ms 75828 KB Output is correct
14 Correct 177 ms 74664 KB Output is correct
15 Incorrect 191 ms 75308 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 334 ms 34220 KB Output is correct
5 Correct 1046 ms 74652 KB Output is correct
6 Correct 638 ms 12588 KB Output is correct
7 Correct 1096 ms 74552 KB Output is correct
8 Correct 646 ms 25808 KB Output is correct
9 Correct 1129 ms 74568 KB Output is correct
10 Correct 845 ms 75712 KB Output is correct
11 Correct 1329 ms 75712 KB Output is correct
12 Correct 1237 ms 75712 KB Output is correct
13 Correct 996 ms 74568 KB Output is correct
14 Correct 1324 ms 75304 KB Output is correct
15 Correct 1084 ms 75792 KB Output is correct
16 Correct 1181 ms 75812 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 0 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 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 3 ms 336 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 17 ms 976 KB Output is correct
28 Correct 19 ms 976 KB Output is correct
29 Correct 19 ms 976 KB Output is correct
30 Correct 24 ms 976 KB Output is correct
31 Correct 22 ms 976 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 108 ms 43284 KB Output is correct
34 Correct 180 ms 74664 KB Output is correct
35 Correct 165 ms 75804 KB Output is correct
36 Correct 182 ms 74772 KB Output is correct
37 Correct 191 ms 75312 KB Output is correct
38 Correct 162 ms 75768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 334 ms 34220 KB Output is correct
5 Correct 1046 ms 74652 KB Output is correct
6 Correct 638 ms 12588 KB Output is correct
7 Correct 1096 ms 74552 KB Output is correct
8 Correct 646 ms 25808 KB Output is correct
9 Correct 1129 ms 74568 KB Output is correct
10 Correct 845 ms 75712 KB Output is correct
11 Correct 1329 ms 75712 KB Output is correct
12 Correct 1237 ms 75712 KB Output is correct
13 Correct 996 ms 74568 KB Output is correct
14 Correct 1324 ms 75304 KB Output is correct
15 Correct 1084 ms 75792 KB Output is correct
16 Correct 1181 ms 75812 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 0 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 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 3 ms 336 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 17 ms 976 KB Output is correct
28 Correct 19 ms 976 KB Output is correct
29 Correct 19 ms 976 KB Output is correct
30 Correct 24 ms 976 KB Output is correct
31 Correct 22 ms 976 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 108 ms 43284 KB Output is correct
34 Correct 180 ms 74664 KB Output is correct
35 Correct 165 ms 75804 KB Output is correct
36 Correct 182 ms 74772 KB Output is correct
37 Correct 191 ms 75312 KB Output is correct
38 Correct 162 ms 75768 KB Output is correct
39 Correct 0 ms 208 KB Output is correct
40 Correct 0 ms 208 KB Output is correct
41 Correct 1 ms 208 KB Output is correct
42 Correct 228 ms 34288 KB Output is correct
43 Correct 1032 ms 74580 KB Output is correct
44 Correct 705 ms 12496 KB Output is correct
45 Correct 1066 ms 74592 KB Output is correct
46 Correct 536 ms 25800 KB Output is correct
47 Correct 1138 ms 74640 KB Output is correct
48 Correct 1016 ms 75784 KB Output is correct
49 Correct 1208 ms 75712 KB Output is correct
50 Correct 1114 ms 75704 KB Output is correct
51 Correct 686 ms 74660 KB Output is correct
52 Correct 1484 ms 75208 KB Output is correct
53 Correct 704 ms 75752 KB Output is correct
54 Correct 1122 ms 75732 KB Output is correct
55 Correct 0 ms 208 KB Output is correct
56 Correct 227 ms 74400 KB Output is correct
57 Correct 1124 ms 74664 KB Output is correct
58 Correct 602 ms 13264 KB Output is correct
59 Correct 1214 ms 74552 KB Output is correct
60 Correct 503 ms 26704 KB Output is correct
61 Correct 1067 ms 74664 KB Output is correct
62 Correct 1093 ms 75808 KB Output is correct
63 Correct 1292 ms 75780 KB Output is correct
64 Correct 1258 ms 75416 KB Output is correct
65 Correct 1192 ms 74660 KB Output is correct
66 Correct 1546 ms 75204 KB Output is correct
67 Correct 1203 ms 75800 KB Output is correct
68 Correct 1204 ms 75816 KB Output is correct
69 Correct 0 ms 208 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 3 ms 336 KB Output is correct
72 Correct 2 ms 336 KB Output is correct
73 Correct 2 ms 336 KB Output is correct
74 Correct 2 ms 336 KB Output is correct
75 Correct 3 ms 336 KB Output is correct
76 Correct 0 ms 208 KB Output is correct
77 Correct 0 ms 208 KB Output is correct
78 Correct 2 ms 208 KB Output is correct
79 Correct 2 ms 336 KB Output is correct
80 Correct 3 ms 336 KB Output is correct
81 Correct 3 ms 336 KB Output is correct
82 Correct 3 ms 336 KB Output is correct
83 Correct 3 ms 336 KB Output is correct
84 Correct 1 ms 336 KB Output is correct
85 Correct 3 ms 336 KB Output is correct
86 Correct 19 ms 976 KB Output is correct
87 Correct 18 ms 976 KB Output is correct
88 Correct 20 ms 976 KB Output is correct
89 Correct 24 ms 976 KB Output is correct
90 Correct 17 ms 976 KB Output is correct
91 Correct 0 ms 336 KB Output is correct
92 Correct 1 ms 464 KB Output is correct
93 Correct 18 ms 976 KB Output is correct
94 Correct 21 ms 976 KB Output is correct
95 Correct 21 ms 976 KB Output is correct
96 Correct 23 ms 976 KB Output is correct
97 Correct 19 ms 976 KB Output is correct
98 Correct 0 ms 208 KB Output is correct
99 Correct 164 ms 74524 KB Output is correct
100 Correct 164 ms 74672 KB Output is correct
101 Correct 162 ms 75720 KB Output is correct
102 Correct 173 ms 74548 KB Output is correct
103 Correct 192 ms 75208 KB Output is correct
104 Correct 168 ms 75824 KB Output is correct
105 Correct 0 ms 208 KB Output is correct
106 Correct 98 ms 43348 KB Output is correct
107 Correct 162 ms 74552 KB Output is correct
108 Correct 161 ms 75712 KB Output is correct
109 Correct 165 ms 74664 KB Output is correct
110 Correct 189 ms 75220 KB Output is correct
111 Correct 161 ms 75792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 222 ms 60564 KB Output is correct
4 Correct 1097 ms 75804 KB Output is correct
5 Correct 993 ms 38364 KB Output is correct
6 Correct 1134 ms 75780 KB Output is correct
7 Correct 884 ms 52148 KB Output is correct
8 Correct 1062 ms 75768 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 3 ms 208 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 3 ms 336 KB Output is correct
16 Correct 3 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 3 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 3 ms 336 KB Output is correct
21 Correct 3 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 3 ms 336 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 4 ms 336 KB Output is correct
26 Correct 0 ms 208 KB Output is correct
27 Correct 1 ms 208 KB Output is correct
28 Correct 3 ms 336 KB Output is correct
29 Correct 3 ms 336 KB Output is correct
30 Correct 3 ms 336 KB Output is correct
31 Correct 4 ms 336 KB Output is correct
32 Correct 4 ms 336 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 0 ms 296 KB Output is correct
35 Correct 2 ms 208 KB Output is correct
36 Correct 3 ms 336 KB Output is correct
37 Correct 4 ms 336 KB Output is correct
38 Correct 3 ms 336 KB Output is correct
39 Correct 3 ms 336 KB Output is correct
40 Correct 3 ms 336 KB Output is correct
41 Correct 1 ms 208 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 1 ms 336 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 208 KB Output is correct
49 Correct 1 ms 208 KB Output is correct
50 Correct 1 ms 208 KB Output is correct
51 Correct 0 ms 208 KB Output is correct
52 Correct 1 ms 208 KB Output is correct
53 Correct 3 ms 336 KB Output is correct
54 Correct 3 ms 336 KB Output is correct
55 Correct 4 ms 336 KB Output is correct
56 Correct 2 ms 336 KB Output is correct
57 Correct 3 ms 336 KB Output is correct
58 Correct 3 ms 336 KB Output is correct
59 Correct 3 ms 336 KB Output is correct
60 Correct 2 ms 336 KB Output is correct
61 Correct 4 ms 336 KB Output is correct
62 Correct 3 ms 336 KB Output is correct
63 Correct 3 ms 336 KB Output is correct
64 Correct 3 ms 336 KB Output is correct
65 Correct 1 ms 336 KB Output is correct
66 Correct 15 ms 848 KB Output is correct
67 Incorrect 20 ms 976 KB Output isn't correct
68 Halted 0 ms 0 KB -