답안 #672320

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
672320 2022-12-15T13:54:32 Z Vahe 밀림 점프 (APIO21_jumps) C++17
25 / 100
4000 ms 65964 KB
#include "jumps.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int n, a, b, c, d;
vector<int> h, pge, nge, dp;
vector<vector<int>> gp;
bool arajitest = 1;
vector<vector<int>> up1, up2;


vector<int> pge_(vector<int> v)
{
    vector<int> tmp(v.size());
    stack<int> s;
    s.push(0);
    tmp[0] = -1;
    for (int i = 1; i < v.size(); i++)
    {
        while (!s.empty() && v[s.top()] < v[i]) s.pop();
        tmp[i] = (s.empty() ? -1 : s.top());
        s.push(i);
    }
    return tmp;
}
vector<int> nge_(vector<int> v)
{
    stack<int> s;
    vector<int> tmp(v.size(), -1);
    for (int i = 0; i < n; i++)
    {
        while (!s.empty() && v[s.top()] < v[i]) tmp[s.top()] = i, s.pop();
        s.push(i);
    }
    return tmp;
}
void dfs(int u)
{
    if (u >= c && u <= d) { dp[u] = 0; return; }
    if (dp[u] != 2e9) return;


    if (gp[u].size() > 0) dfs(gp[u][0]);
    if (gp[u].size() > 1) dfs(gp[u][1]);


    int x = (gp[u].size() > 0 ? dp[gp[u][0]] : 2e9);
    int y = (gp[u].size() > 1 ? dp[gp[u][1]] : 2e9);
    dp[u] = min(x, y) + 1;
}

void make1(int u, int p)
{
    up1[u][0] = p;
    for (int i = 1; i < 20; i++) up1[u][i - 1] != 2e9 ? up1[u][i] = up1[up1[u][i - 1]][i - 1] : 1;

    int x = (gp[u].size() > 0 ? gp[u][0] : 2e9);
    int y = (gp[u].size() > 1 ? gp[u][1] : 2e9);
    
    int poqr, mec;
    if (x == 2e9) poqr = 2e9;
    else poqr = h[x];
    if (y == 2e9) mec = 2e9;
    else mec = h[y];

    if (poqr > mec) swap(poqr, mec), swap(x, y);
    if (mec != 2e9) make1(y, u);
    else if (poqr != 2e9) make1(x, u);
}

void make2(int u, int p)
{
    up2[u][0] = p;
    for (int i = 1; i < 20; i++) up2[u][i - 1] != 2e9 ? up2[u][i] = up2[up2[u][i - 1]][i - 1] : 1;


    int x = (gp[u].size() > 0 ? gp[u][0] : 2e9);
    int y = (gp[u].size() > 1 ? gp[u][1] : 2e9);
    int poqr = x != 2e9 ? h[x] : 2e9;
    int mec = y != 2e9 ? h[y] : 2e9;
    if (poqr < mec) swap(poqr, mec), swap(x, y);
    if (poqr != 2e9) make2(x, u);
    else if (mec != 2e9) make2(y, u);
}

void init(int N, vector<int> H)
{
    n = N;
    h = H;
    pge = pge_(H);
    nge = nge_(H);
    gp = vector<vector<int>>(n);
    for (int i = 0; i < n; i++)
    {
        if (pge[i] != -1) gp[i].push_back(pge[i]);
        if (nge[i] != -1) gp[i].push_back(nge[i]);
    }
    for (int i = 0; i < n; i++) if (H[i] != i + 1) arajitest = 0;
}


int minimum_jumps(int A, int B, int C, int D)
{
    a = A, b = B, c = C, d = D;
    if (arajitest) return C - B;
    else if (a == b && c == d && n > 2000) {
        up1 = up2 = vector<vector<int>>(n, vector<int>(20, 2e9));
        make1(A, 2e9);
        make2(A, 2e9);

        int G = A, len = 0;
        for (int i = 19; i >= 0; i--)
        {
            if (up1[G][i] != 2e9 && h[up1[G][i]] <= h[C]) G = up1[G][i], len += (1 << i);
        }
        if (G == C) return len;
        for (int i = 19; i >= 0; i--)
        {
            if (up2[G][i] != 2e9 && h[up2[G][i]] <= h[C]) G = up2[G][i], len += (1 << i);
        }
        return G == C ? len : -1;
    }
    else {
        dp = vector<int>(n, 2e9);
        int mn = 2e9;
        for (int i = a; i <= b; i++) dfs(i), mn = min(mn, dp[i]);
        return (mn >= 2e9 ? -1 : mn);
    }
}

Compilation message

jumps.cpp: In function 'std::vector<int> pge_(std::vector<int>)':
jumps.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 1; i < v.size(); i++)
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 124 ms 12068 KB Output is correct
4 Correct 594 ms 15116 KB Output is correct
5 Correct 694 ms 7740 KB Output is correct
6 Correct 612 ms 15136 KB Output is correct
7 Correct 764 ms 10416 KB Output is correct
8 Correct 641 ms 15168 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 1 ms 208 KB Output is correct
5 Correct 2 ms 208 KB Output is correct
6 Correct 3 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 3 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 3 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 3 ms 208 KB Output is correct
13 Correct 3 ms 208 KB Output is correct
14 Correct 3 ms 208 KB Output is correct
15 Correct 3 ms 208 KB Output is correct
16 Correct 2 ms 220 KB Output is correct
17 Correct 2 ms 208 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 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 4 ms 208 KB Output is correct
23 Correct 2 ms 208 KB Output is correct
24 Correct 4 ms 208 KB Output is correct
25 Correct 1 ms 208 KB Output is correct
26 Correct 0 ms 208 KB Output is correct
27 Correct 2 ms 208 KB Output is correct
28 Correct 3 ms 208 KB Output is correct
29 Correct 2 ms 208 KB Output is correct
30 Correct 2 ms 208 KB Output is correct
31 Correct 2 ms 208 KB Output is correct
32 Correct 2 ms 208 KB Output is correct
33 Correct 0 ms 208 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 0 ms 208 KB Output is correct
37 Correct 1 ms 208 KB Output is correct
38 Correct 1 ms 208 KB Output is correct
39 Correct 0 ms 208 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 1 ms 208 KB Output is correct
5 Correct 2 ms 208 KB Output is correct
6 Correct 3 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 3 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 3 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 3 ms 208 KB Output is correct
13 Correct 3 ms 208 KB Output is correct
14 Correct 3 ms 208 KB Output is correct
15 Correct 3 ms 208 KB Output is correct
16 Correct 2 ms 220 KB Output is correct
17 Correct 2 ms 208 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 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 4 ms 208 KB Output is correct
23 Correct 2 ms 208 KB Output is correct
24 Correct 4 ms 208 KB Output is correct
25 Correct 1 ms 208 KB Output is correct
26 Correct 0 ms 208 KB Output is correct
27 Correct 2 ms 208 KB Output is correct
28 Correct 3 ms 208 KB Output is correct
29 Correct 2 ms 208 KB Output is correct
30 Correct 2 ms 208 KB Output is correct
31 Correct 2 ms 208 KB Output is correct
32 Correct 2 ms 208 KB Output is correct
33 Correct 0 ms 208 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 0 ms 208 KB Output is correct
37 Correct 1 ms 208 KB Output is correct
38 Correct 1 ms 208 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 0 ms 208 KB Output is correct
43 Correct 0 ms 208 KB Output is correct
44 Correct 2 ms 208 KB Output is correct
45 Correct 3 ms 208 KB Output is correct
46 Correct 2 ms 208 KB Output is correct
47 Correct 3 ms 208 KB Output is correct
48 Correct 1 ms 208 KB Output is correct
49 Correct 2 ms 208 KB Output is correct
50 Correct 2 ms 208 KB Output is correct
51 Correct 3 ms 208 KB Output is correct
52 Correct 2 ms 208 KB Output is correct
53 Correct 3 ms 208 KB Output is correct
54 Correct 2 ms 208 KB Output is correct
55 Correct 2 ms 208 KB Output is correct
56 Correct 3 ms 208 KB Output is correct
57 Correct 0 ms 208 KB Output is correct
58 Correct 29 ms 336 KB Output is correct
59 Correct 40 ms 336 KB Output is correct
60 Correct 6 ms 336 KB Output is correct
61 Correct 42 ms 336 KB Output is correct
62 Correct 15 ms 208 KB Output is correct
63 Correct 47 ms 336 KB Output is correct
64 Correct 19 ms 336 KB Output is correct
65 Correct 40 ms 464 KB Output is correct
66 Correct 41 ms 464 KB Output is correct
67 Correct 39 ms 336 KB Output is correct
68 Correct 57 ms 464 KB Output is correct
69 Correct 45 ms 464 KB Output is correct
70 Correct 38 ms 336 KB Output is correct
71 Correct 0 ms 208 KB Output is correct
72 Correct 1 ms 208 KB Output is correct
73 Correct 3 ms 208 KB Output is correct
74 Correct 2 ms 208 KB Output is correct
75 Correct 2 ms 208 KB Output is correct
76 Correct 3 ms 208 KB Output is correct
77 Correct 3 ms 208 KB Output is correct
78 Correct 0 ms 208 KB Output is correct
79 Correct 0 ms 208 KB Output is correct
80 Correct 1 ms 208 KB Output is correct
81 Correct 2 ms 208 KB Output is correct
82 Correct 2 ms 208 KB Output is correct
83 Correct 2 ms 208 KB Output is correct
84 Correct 2 ms 208 KB Output is correct
85 Correct 2 ms 208 KB Output is correct
86 Correct 1 ms 208 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 1 ms 208 KB Output is correct
89 Correct 1 ms 208 KB Output is correct
90 Correct 1 ms 208 KB Output is correct
91 Correct 0 ms 208 KB Output is correct
92 Correct 1 ms 208 KB Output is correct
93 Correct 0 ms 208 KB Output is correct
94 Correct 4 ms 208 KB Output is correct
95 Correct 40 ms 336 KB Output is correct
96 Correct 43 ms 464 KB Output is correct
97 Correct 37 ms 336 KB Output is correct
98 Correct 49 ms 464 KB Output is correct
99 Correct 33 ms 560 KB Output is correct
100 Correct 0 ms 208 KB Output is correct
101 Correct 1 ms 336 KB Output is correct
102 Correct 22 ms 336 KB Output is correct
103 Correct 33 ms 464 KB Output is correct
104 Correct 24 ms 336 KB Output is correct
105 Correct 40 ms 464 KB Output is correct
106 Correct 40 ms 464 KB Output is correct
107 Correct 0 ms 208 KB Output is correct
108 Correct 1 ms 336 KB Output is correct
109 Correct 1 ms 336 KB Output is correct
110 Correct 1 ms 464 KB Output is correct
111 Correct 1 ms 336 KB Output is correct
112 Correct 1 ms 464 KB Output is correct
113 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 72 ms 49928 KB Output is correct
6 Correct 106 ms 62844 KB Output is correct
7 Correct 43 ms 31432 KB Output is correct
8 Correct 93 ms 62840 KB Output is correct
9 Correct 13 ms 9704 KB Output is correct
10 Correct 97 ms 62844 KB Output is correct
11 Correct 33 ms 15160 KB Output is correct
12 Incorrect 91 ms 65964 KB Output isn't correct
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Execution timed out 4070 ms 38940 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Execution timed out 4070 ms 38940 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 124 ms 12068 KB Output is correct
4 Correct 594 ms 15116 KB Output is correct
5 Correct 694 ms 7740 KB Output is correct
6 Correct 612 ms 15136 KB Output is correct
7 Correct 764 ms 10416 KB Output is correct
8 Correct 641 ms 15168 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 1 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 3 ms 208 KB Output is correct
15 Correct 2 ms 208 KB Output is correct
16 Correct 3 ms 208 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 3 ms 208 KB Output is correct
19 Correct 2 ms 208 KB Output is correct
20 Correct 3 ms 208 KB Output is correct
21 Correct 3 ms 208 KB Output is correct
22 Correct 3 ms 208 KB Output is correct
23 Correct 3 ms 208 KB Output is correct
24 Correct 2 ms 220 KB Output is correct
25 Correct 2 ms 208 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 208 KB Output is correct
29 Correct 3 ms 208 KB Output is correct
30 Correct 4 ms 208 KB Output is correct
31 Correct 2 ms 208 KB Output is correct
32 Correct 4 ms 208 KB Output is correct
33 Correct 1 ms 208 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 2 ms 208 KB Output is correct
36 Correct 3 ms 208 KB Output is correct
37 Correct 2 ms 208 KB Output is correct
38 Correct 2 ms 208 KB Output is correct
39 Correct 2 ms 208 KB Output is correct
40 Correct 2 ms 208 KB Output is correct
41 Correct 0 ms 208 KB Output is correct
42 Correct 0 ms 208 KB Output is correct
43 Correct 1 ms 208 KB Output is correct
44 Correct 0 ms 208 KB Output is correct
45 Correct 1 ms 208 KB Output is correct
46 Correct 1 ms 208 KB Output is correct
47 Correct 0 ms 208 KB Output is correct
48 Correct 0 ms 208 KB Output is correct
49 Correct 0 ms 208 KB Output is correct
50 Correct 0 ms 208 KB Output is correct
51 Correct 0 ms 208 KB Output is correct
52 Correct 2 ms 208 KB Output is correct
53 Correct 3 ms 208 KB Output is correct
54 Correct 2 ms 208 KB Output is correct
55 Correct 3 ms 208 KB Output is correct
56 Correct 1 ms 208 KB Output is correct
57 Correct 2 ms 208 KB Output is correct
58 Correct 2 ms 208 KB Output is correct
59 Correct 3 ms 208 KB Output is correct
60 Correct 2 ms 208 KB Output is correct
61 Correct 3 ms 208 KB Output is correct
62 Correct 2 ms 208 KB Output is correct
63 Correct 2 ms 208 KB Output is correct
64 Correct 3 ms 208 KB Output is correct
65 Correct 0 ms 208 KB Output is correct
66 Correct 29 ms 336 KB Output is correct
67 Correct 40 ms 336 KB Output is correct
68 Correct 6 ms 336 KB Output is correct
69 Correct 42 ms 336 KB Output is correct
70 Correct 15 ms 208 KB Output is correct
71 Correct 47 ms 336 KB Output is correct
72 Correct 19 ms 336 KB Output is correct
73 Correct 40 ms 464 KB Output is correct
74 Correct 41 ms 464 KB Output is correct
75 Correct 39 ms 336 KB Output is correct
76 Correct 57 ms 464 KB Output is correct
77 Correct 45 ms 464 KB Output is correct
78 Correct 38 ms 336 KB Output is correct
79 Correct 0 ms 208 KB Output is correct
80 Correct 1 ms 208 KB Output is correct
81 Correct 3 ms 208 KB Output is correct
82 Correct 2 ms 208 KB Output is correct
83 Correct 2 ms 208 KB Output is correct
84 Correct 3 ms 208 KB Output is correct
85 Correct 3 ms 208 KB Output is correct
86 Correct 0 ms 208 KB Output is correct
87 Correct 0 ms 208 KB Output is correct
88 Correct 1 ms 208 KB Output is correct
89 Correct 2 ms 208 KB Output is correct
90 Correct 2 ms 208 KB Output is correct
91 Correct 2 ms 208 KB Output is correct
92 Correct 2 ms 208 KB Output is correct
93 Correct 2 ms 208 KB Output is correct
94 Correct 1 ms 208 KB Output is correct
95 Correct 0 ms 208 KB Output is correct
96 Correct 1 ms 208 KB Output is correct
97 Correct 1 ms 208 KB Output is correct
98 Correct 1 ms 208 KB Output is correct
99 Correct 0 ms 208 KB Output is correct
100 Correct 1 ms 208 KB Output is correct
101 Correct 0 ms 208 KB Output is correct
102 Correct 4 ms 208 KB Output is correct
103 Correct 40 ms 336 KB Output is correct
104 Correct 43 ms 464 KB Output is correct
105 Correct 37 ms 336 KB Output is correct
106 Correct 49 ms 464 KB Output is correct
107 Correct 33 ms 560 KB Output is correct
108 Correct 0 ms 208 KB Output is correct
109 Correct 1 ms 336 KB Output is correct
110 Correct 22 ms 336 KB Output is correct
111 Correct 33 ms 464 KB Output is correct
112 Correct 24 ms 336 KB Output is correct
113 Correct 40 ms 464 KB Output is correct
114 Correct 40 ms 464 KB Output is correct
115 Correct 0 ms 208 KB Output is correct
116 Correct 1 ms 336 KB Output is correct
117 Correct 1 ms 336 KB Output is correct
118 Correct 1 ms 464 KB Output is correct
119 Correct 1 ms 336 KB Output is correct
120 Correct 1 ms 464 KB Output is correct
121 Correct 1 ms 336 KB Output is correct
122 Correct 0 ms 208 KB Output is correct
123 Correct 0 ms 208 KB Output is correct
124 Correct 0 ms 208 KB Output is correct
125 Correct 0 ms 208 KB Output is correct
126 Correct 72 ms 49928 KB Output is correct
127 Correct 106 ms 62844 KB Output is correct
128 Correct 43 ms 31432 KB Output is correct
129 Correct 93 ms 62840 KB Output is correct
130 Correct 13 ms 9704 KB Output is correct
131 Correct 97 ms 62844 KB Output is correct
132 Correct 33 ms 15160 KB Output is correct
133 Incorrect 91 ms 65964 KB Output isn't correct
134 Halted 0 ms 0 KB -