답안 #438077

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
438077 2021-06-27T13:49:26 Z cheeheng 밀림 점프 (APIO21_jumps) C++14
12 / 100
4000 ms 83512 KB
#include "jumps.h"
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;

int nextGreater[200005][20];
int prevGreater[200005][20];
int indx[200005];
int log2_[200005];
ii val[200005][20];
int best[200005][20];
int A1[200005];

int M;

void init2(){
    for(int i = 0; i < M; i ++){
        val[i][0] = ii(A1[i], i);
    }

    for(int k = 1; k < 20; k ++){
        for(int i = 0; i <= M-(1<<k); i ++){
            val[i][k] = max(val[i][k-1], val[i+(1<<(k-1))][k-1]);
        }
    }

    log2_[1] = 0;
    for(int i = 2; i <= M; i ++){
        log2_[i] = log2_[i>>1] + 1;
    }
}

ii findMax(int l, int r){
    int k = log2_[r-l+1];
    return max(val[l][k], val[r-(1<<k)+1][k]);
}

void init(int N, std::vector<int> H){
    M = N;

    for(int i = 0; i < N; i ++){
        A1[i] = H[i];
    }

    init2();

    for(int i = 0; i < N; i ++){
        nextGreater[i][0] = N;
        prevGreater[i][0] = -1;
    }

    vector<ii> stack1 = vector<ii>();
    for(int i = 0; i < N; i ++){
        while(!stack1.empty()){
            ii temp = stack1.back();

            if(temp.first < H[i]){
                nextGreater[temp.second][0] = i;
                stack1.pop_back();
            }else{
                break;
            }
        }
        stack1.emplace_back(H[i], i);
    }

    stack1 = vector<ii>();
    for(int i = N-1; i >= 0; i --){
        while(!stack1.empty()){
            ii temp = stack1.back();

            if(temp.first < H[i]){
                prevGreater[temp.second][0] = i;
                stack1.pop_back();
            }else{
                break;
            }
        }
        stack1.emplace_back(H[i], i);
    }

    /*for(int i = 0; i < N; i ++){
        printf("nextGreater[%d]=%d\n", i, nextGreater[i][0]);
    }

    for(int i = 0; i < N; i ++){
        printf("prevGreater[%d]=%d\n", i, prevGreater[i][0]);
    }*/

    for(int k = 1; k < 20; k ++){
        for(int i = 0; i < N; i ++){
            if(nextGreater[i][k-1] == N){
                nextGreater[i][k] = N;
            }else{
                nextGreater[i][k] = nextGreater[nextGreater[i][k-1]][k-1];
            }

            if(prevGreater[i][k-1] == -1){
                prevGreater[i][k] = -1;
            }else{
                prevGreater[i][k] = prevGreater[prevGreater[i][k-1]][k-1];
            }
        }
    }

    for(int i = 0; i < N; i ++){
        int X2 = i;
        int X3, X4;
        if(prevGreater[X2][0] == -1){
            X3 = nextGreater[X2][0];
            X4 = prevGreater[X2][0];
        }else if(nextGreater[X2][0] == M){
            X3 = prevGreater[X2][0];
            X4 = nextGreater[X2][0];
        }else if(A1[prevGreater[X2][0]] > A1[nextGreater[X2][0]]){
            X3 = prevGreater[X2][0];
            X4 = nextGreater[X2][0];
        }else{
            X3 = nextGreater[X2][0];
            X4 = prevGreater[X2][0];
        }
        best[i][0] = X3;
        if(best[i][0] == M){best[i][0] = -1;}
    }

    for(int k = 1; k < 20; k ++){
        for(int i = 0; i < N; i ++){
            if(best[i][k-1] == -1){
                best[i][k] = -1;
            }else{
                best[i][k] = best[best[i][k-1]][k-1];
            }
        }
    }
}

int minimum_jumps(int A, int B, int C, int D){
    assert(0 <= A && A <= B && B < C && C <= D && D <= M-1);

    ii maxValCD = findMax(C, D);
    int maxIndxCD = prevGreater[maxValCD.second][0];
    //printf("maxValCD=%d\n", maxValCD);
    A = max(maxIndxCD+1, A);

    if(A > B){return -1;}

    ii maxValAB = findMax(A, B);
    int X = maxValAB.second;

    int cutOffIndx = X;
    for(int k = 19; k >= 0; k --){
        if(nextGreater[cutOffIndx][k] != M && nextGreater[cutOffIndx][k] < C){
            cutOffIndx = nextGreater[cutOffIndx][k];
        }
    }
    cutOffIndx = nextGreater[cutOffIndx][0];
    assert(cutOffIndx >= C && cutOffIndx <= D);

    int ans = 0;

    int X5 = X;
    int totalDist = 0;
    for(int k = 19; k >= 0; k --){
        int temp = best[X5][k];
        if(temp != -1 && val[temp] < val[cutOffIndx]){
            totalDist += (1<<k);
            X5 = temp;
        }
    }

    int X2 = X;
    totalDist = max(0, totalDist-min(200, M));
    ans += totalDist;
    for(int k = 19; k >= 0; k --){
        int temp = best[X2][k];
        if(totalDist >= (1<<k)){
            totalDist -= (1<<k);
            X2 = temp;
        }
        if(X2 == -1){vector<int> MLE(1<<27); return MLE[0];}
    }

    while(X2 < C){
        int X3, X4;
        if(prevGreater[X2][0] == -1){
            X3 = nextGreater[X2][0];
            X4 = prevGreater[X2][0];
        }else if(nextGreater[X2][0] == M){
            X3 = prevGreater[X2][0];
            X4 = nextGreater[X2][0];
        }else if(A1[prevGreater[X2][0]] > A1[nextGreater[X2][0]]){
            X3 = prevGreater[X2][0];
            X4 = nextGreater[X2][0];
        }else{
            X3 = nextGreater[X2][0];
            X4 = prevGreater[X2][0];
        }

        //printf("X2=%d; X3=%d; X4=%d\n", X2, X3, X4);
        if(X4 >= C && X4 <= D){
            X2 = X4;
        }else if(A1[X3] > maxValCD.first){
            X2 = X4;
        }else{
            X2 = X3;
        }
        //printf("X2=%d\n", X2);
        ans ++;
        assert(X2 >= 0 && X2 < M);
    }

    return ans;
}

Compilation message

jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:109:17: warning: variable 'X4' set but not used [-Wunused-but-set-variable]
  109 |         int X3, X4;
      |                 ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 409 ms 66352 KB Output is correct
4 Correct 1975 ms 83368 KB Output is correct
5 Correct 1495 ms 42188 KB Output is correct
6 Correct 1799 ms 83368 KB Output is correct
7 Correct 1623 ms 57136 KB Output is correct
8 Correct 1756 ms 83272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 3 ms 328 KB Output is correct
6 Correct 4 ms 328 KB Output is correct
7 Correct 2 ms 328 KB Output is correct
8 Correct 4 ms 328 KB Output is correct
9 Correct 2 ms 328 KB Output is correct
10 Correct 3 ms 328 KB Output is correct
11 Correct 2 ms 328 KB Output is correct
12 Correct 4 ms 328 KB Output is correct
13 Correct 3 ms 328 KB Output is correct
14 Correct 3 ms 328 KB Output is correct
15 Correct 5 ms 328 KB Output is correct
16 Correct 4 ms 328 KB Output is correct
17 Correct 5 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 4 ms 328 KB Output is correct
21 Correct 3 ms 328 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 3 ms 328 KB Output is correct
24 Correct 4 ms 328 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 1 ms 328 KB Output is correct
27 Correct 2 ms 328 KB Output is correct
28 Correct 3 ms 328 KB Output is correct
29 Correct 4 ms 328 KB Output is correct
30 Correct 4 ms 328 KB Output is correct
31 Correct 4 ms 328 KB Output is correct
32 Correct 4 ms 328 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 1 ms 328 KB Output is correct
35 Correct 1 ms 328 KB Output is correct
36 Correct 1 ms 328 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
38 Correct 1 ms 328 KB Output is correct
39 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 328 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 3 ms 328 KB Output is correct
6 Correct 4 ms 328 KB Output is correct
7 Correct 2 ms 328 KB Output is correct
8 Correct 4 ms 328 KB Output is correct
9 Correct 2 ms 328 KB Output is correct
10 Correct 3 ms 328 KB Output is correct
11 Correct 2 ms 328 KB Output is correct
12 Correct 4 ms 328 KB Output is correct
13 Correct 3 ms 328 KB Output is correct
14 Correct 3 ms 328 KB Output is correct
15 Correct 5 ms 328 KB Output is correct
16 Correct 4 ms 328 KB Output is correct
17 Correct 5 ms 328 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 4 ms 328 KB Output is correct
21 Correct 3 ms 328 KB Output is correct
22 Correct 2 ms 328 KB Output is correct
23 Correct 3 ms 328 KB Output is correct
24 Correct 4 ms 328 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 1 ms 328 KB Output is correct
27 Correct 2 ms 328 KB Output is correct
28 Correct 3 ms 328 KB Output is correct
29 Correct 4 ms 328 KB Output is correct
30 Correct 4 ms 328 KB Output is correct
31 Correct 4 ms 328 KB Output is correct
32 Correct 4 ms 328 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 1 ms 328 KB Output is correct
35 Correct 1 ms 328 KB Output is correct
36 Correct 1 ms 328 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
38 Correct 1 ms 328 KB Output is correct
39 Correct 1 ms 328 KB Output is correct
40 Correct 1 ms 328 KB Output is correct
41 Correct 1 ms 328 KB Output is correct
42 Correct 1 ms 328 KB Output is correct
43 Correct 1 ms 328 KB Output is correct
44 Correct 4 ms 328 KB Output is correct
45 Correct 4 ms 328 KB Output is correct
46 Correct 3 ms 328 KB Output is correct
47 Correct 4 ms 328 KB Output is correct
48 Correct 2 ms 328 KB Output is correct
49 Correct 4 ms 328 KB Output is correct
50 Correct 4 ms 328 KB Output is correct
51 Correct 4 ms 328 KB Output is correct
52 Correct 3 ms 328 KB Output is correct
53 Correct 4 ms 328 KB Output is correct
54 Correct 3 ms 328 KB Output is correct
55 Correct 4 ms 328 KB Output is correct
56 Correct 3 ms 328 KB Output is correct
57 Correct 1 ms 328 KB Output is correct
58 Correct 15 ms 968 KB Output is correct
59 Correct 29 ms 1096 KB Output is correct
60 Correct 11 ms 584 KB Output is correct
61 Correct 29 ms 1096 KB Output is correct
62 Correct 20 ms 328 KB Output is correct
63 Correct 31 ms 1096 KB Output is correct
64 Correct 34 ms 1096 KB Output is correct
65 Correct 30 ms 1096 KB Output is correct
66 Correct 27 ms 1096 KB Output is correct
67 Correct 29 ms 1096 KB Output is correct
68 Incorrect 22 ms 1096 KB Output isn't correct
69 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 190 ms 65772 KB Output is correct
6 Correct 241 ms 81580 KB Output is correct
7 Correct 119 ms 41920 KB Output is correct
8 Correct 238 ms 81816 KB Output is correct
9 Correct 18 ms 12488 KB Output is correct
10 Correct 251 ms 81728 KB Output is correct
11 Correct 237 ms 83332 KB Output is correct
12 Correct 221 ms 82952 KB Output is correct
13 Correct 246 ms 82940 KB Output is correct
14 Correct 218 ms 81580 KB Output is correct
15 Incorrect 228 ms 83512 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 408 ms 37508 KB Output is correct
5 Correct 1296 ms 81824 KB Output is correct
6 Correct 1012 ms 13656 KB Output is correct
7 Correct 1157 ms 81704 KB Output is correct
8 Correct 553 ms 28332 KB Output is correct
9 Correct 1351 ms 81688 KB Output is correct
10 Correct 2160 ms 83416 KB Output is correct
11 Execution timed out 4054 ms 83224 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 408 ms 37508 KB Output is correct
5 Correct 1296 ms 81824 KB Output is correct
6 Correct 1012 ms 13656 KB Output is correct
7 Correct 1157 ms 81704 KB Output is correct
8 Correct 553 ms 28332 KB Output is correct
9 Correct 1351 ms 81688 KB Output is correct
10 Correct 2160 ms 83416 KB Output is correct
11 Execution timed out 4054 ms 83224 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 409 ms 66352 KB Output is correct
4 Correct 1975 ms 83368 KB Output is correct
5 Correct 1495 ms 42188 KB Output is correct
6 Correct 1799 ms 83368 KB Output is correct
7 Correct 1623 ms 57136 KB Output is correct
8 Correct 1756 ms 83272 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 0 ms 328 KB Output is correct
12 Correct 1 ms 200 KB Output is correct
13 Correct 3 ms 328 KB Output is correct
14 Correct 4 ms 328 KB Output is correct
15 Correct 2 ms 328 KB Output is correct
16 Correct 4 ms 328 KB Output is correct
17 Correct 2 ms 328 KB Output is correct
18 Correct 3 ms 328 KB Output is correct
19 Correct 2 ms 328 KB Output is correct
20 Correct 4 ms 328 KB Output is correct
21 Correct 3 ms 328 KB Output is correct
22 Correct 3 ms 328 KB Output is correct
23 Correct 5 ms 328 KB Output is correct
24 Correct 4 ms 328 KB Output is correct
25 Correct 5 ms 328 KB Output is correct
26 Correct 1 ms 328 KB Output is correct
27 Correct 1 ms 328 KB Output is correct
28 Correct 4 ms 328 KB Output is correct
29 Correct 3 ms 328 KB Output is correct
30 Correct 2 ms 328 KB Output is correct
31 Correct 3 ms 328 KB Output is correct
32 Correct 4 ms 328 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 1 ms 328 KB Output is correct
35 Correct 2 ms 328 KB Output is correct
36 Correct 3 ms 328 KB Output is correct
37 Correct 4 ms 328 KB Output is correct
38 Correct 4 ms 328 KB Output is correct
39 Correct 4 ms 328 KB Output is correct
40 Correct 4 ms 328 KB Output is correct
41 Correct 1 ms 328 KB Output is correct
42 Correct 1 ms 328 KB Output is correct
43 Correct 1 ms 328 KB Output is correct
44 Correct 1 ms 328 KB Output is correct
45 Correct 1 ms 328 KB Output is correct
46 Correct 1 ms 328 KB Output is correct
47 Correct 1 ms 328 KB Output is correct
48 Correct 1 ms 328 KB Output is correct
49 Correct 1 ms 328 KB Output is correct
50 Correct 1 ms 328 KB Output is correct
51 Correct 1 ms 328 KB Output is correct
52 Correct 4 ms 328 KB Output is correct
53 Correct 4 ms 328 KB Output is correct
54 Correct 3 ms 328 KB Output is correct
55 Correct 4 ms 328 KB Output is correct
56 Correct 2 ms 328 KB Output is correct
57 Correct 4 ms 328 KB Output is correct
58 Correct 4 ms 328 KB Output is correct
59 Correct 4 ms 328 KB Output is correct
60 Correct 3 ms 328 KB Output is correct
61 Correct 4 ms 328 KB Output is correct
62 Correct 3 ms 328 KB Output is correct
63 Correct 4 ms 328 KB Output is correct
64 Correct 3 ms 328 KB Output is correct
65 Correct 1 ms 328 KB Output is correct
66 Correct 15 ms 968 KB Output is correct
67 Correct 29 ms 1096 KB Output is correct
68 Correct 11 ms 584 KB Output is correct
69 Correct 29 ms 1096 KB Output is correct
70 Correct 20 ms 328 KB Output is correct
71 Correct 31 ms 1096 KB Output is correct
72 Correct 34 ms 1096 KB Output is correct
73 Correct 30 ms 1096 KB Output is correct
74 Correct 27 ms 1096 KB Output is correct
75 Correct 29 ms 1096 KB Output is correct
76 Incorrect 22 ms 1096 KB Output isn't correct
77 Halted 0 ms 0 KB -