답안 #710770

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710770 2023-03-15T18:13:31 Z Jarif_Rahman Homework (CEOI22_homework) C++17
100 / 100
317 ms 217840 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

str s;
int n = 1, m;
vector<int> type(1, -1);
vector<int> L(1, -1), R(1, -1);
vector<int> leaves;
vector<vector<int>> dp;
vector<int> sum;

int create_tree(int nd, int i){
    if(s[i] == '?'){
        leaves.pb(nd);
        return i;
    }
    if(s[i+1] == 'i') type[nd] = 0;
    else type[nd] = 1;

    L.pb(-1);
    R.pb(-1);
    type.pb(-1);
    L[nd] = n;
    n++;

    int x = create_tree(L[nd], i+4);
    x+=2;

    L.pb(-1);
    R.pb(-1);
    type.pb(-1);
    R[nd] = n;
    n++;

    x = create_tree(R[nd], x);

    return x+1;
}

void pre_dfs(int nd){
    if(type[nd] == -1){
        dp[nd][0] = 1;
        dp[nd][1] = 1;
        return;
    }
    pre_dfs(L[nd]);
    pre_dfs(R[nd]);

    // want smaller (dp[nd][0])
    if(type[nd] == 0) dp[nd][0] = min(dp[L[nd]][0], dp[R[nd]][0]);
    else dp[nd][0] = dp[L[nd]][0] + dp[R[nd]][0];

    // want larger (dp[nd][1])
    if(type[nd] == 0) dp[nd][1] = dp[L[nd]][1] + dp[R[nd]][1];
    else dp[nd][1] = min(dp[L[nd]][1], dp[R[nd]][1]);
}

void dfs(int nd, int a = 0, int b = 0){
    if(type[nd] == -1){
        if(a+b < m) sum[a]++, sum[m-b]--;
        return;
    }

    if(type[nd] == 0){
        dfs(L[nd], a, b+dp[R[nd]][1]);
        dfs(R[nd], a, b+dp[L[nd]][1]);
    }
    else{
        dfs(L[nd], a+dp[R[nd]][0], b);
        dfs(R[nd], a+dp[L[nd]][0], b);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> s;
    create_tree(0, 0);

    dp.assign(n, vector<int>(2, n));
    m = leaves.size();
    sum.assign(m+1, 0);

    pre_dfs(0);
    dfs(0);

    int ans = 0;
    for(int i = 0; i < m; i++){
        if(i) sum[i]+=sum[i-1];
        if(sum[i] > 0) ans++;
    }

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 324 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 324 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 320 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 320 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 320 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 320 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 274 ms 217676 KB Output is correct
2 Correct 317 ms 217736 KB Output is correct
3 Correct 283 ms 217744 KB Output is correct
4 Correct 304 ms 217672 KB Output is correct
5 Correct 268 ms 217724 KB Output is correct
6 Correct 276 ms 217720 KB Output is correct
7 Correct 270 ms 217728 KB Output is correct
8 Correct 278 ms 217716 KB Output is correct
9 Correct 267 ms 217736 KB Output is correct
10 Correct 290 ms 217740 KB Output is correct
11 Correct 286 ms 217728 KB Output is correct
12 Correct 301 ms 217728 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 324 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 320 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 320 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 320 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 320 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 1 ms 448 KB Output is correct
58 Correct 1 ms 468 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 448 KB Output is correct
61 Correct 1 ms 452 KB Output is correct
62 Correct 1 ms 456 KB Output is correct
63 Correct 1 ms 484 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 1 ms 468 KB Output is correct
68 Correct 1 ms 468 KB Output is correct
69 Correct 1 ms 452 KB Output is correct
70 Correct 1 ms 452 KB Output is correct
71 Correct 1 ms 468 KB Output is correct
72 Correct 1 ms 468 KB Output is correct
73 Correct 1 ms 468 KB Output is correct
74 Correct 1 ms 468 KB Output is correct
75 Correct 1 ms 468 KB Output is correct
76 Correct 1 ms 468 KB Output is correct
77 Correct 1 ms 468 KB Output is correct
78 Correct 1 ms 448 KB Output is correct
79 Correct 1 ms 456 KB Output is correct
80 Correct 1 ms 468 KB Output is correct
81 Correct 1 ms 468 KB Output is correct
82 Correct 1 ms 468 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 320 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 324 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 320 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 320 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 320 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 1 ms 320 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 324 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 274 ms 217676 KB Output is correct
58 Correct 317 ms 217736 KB Output is correct
59 Correct 283 ms 217744 KB Output is correct
60 Correct 304 ms 217672 KB Output is correct
61 Correct 268 ms 217724 KB Output is correct
62 Correct 276 ms 217720 KB Output is correct
63 Correct 270 ms 217728 KB Output is correct
64 Correct 278 ms 217716 KB Output is correct
65 Correct 267 ms 217736 KB Output is correct
66 Correct 290 ms 217740 KB Output is correct
67 Correct 286 ms 217728 KB Output is correct
68 Correct 301 ms 217728 KB Output is correct
69 Correct 1 ms 320 KB Output is correct
70 Correct 1 ms 448 KB Output is correct
71 Correct 1 ms 468 KB Output is correct
72 Correct 1 ms 468 KB Output is correct
73 Correct 1 ms 448 KB Output is correct
74 Correct 1 ms 452 KB Output is correct
75 Correct 1 ms 456 KB Output is correct
76 Correct 1 ms 484 KB Output is correct
77 Correct 1 ms 468 KB Output is correct
78 Correct 1 ms 468 KB Output is correct
79 Correct 1 ms 468 KB Output is correct
80 Correct 1 ms 468 KB Output is correct
81 Correct 1 ms 468 KB Output is correct
82 Correct 1 ms 452 KB Output is correct
83 Correct 1 ms 452 KB Output is correct
84 Correct 1 ms 468 KB Output is correct
85 Correct 1 ms 468 KB Output is correct
86 Correct 1 ms 468 KB Output is correct
87 Correct 1 ms 468 KB Output is correct
88 Correct 1 ms 468 KB Output is correct
89 Correct 1 ms 468 KB Output is correct
90 Correct 1 ms 468 KB Output is correct
91 Correct 1 ms 448 KB Output is correct
92 Correct 1 ms 456 KB Output is correct
93 Correct 1 ms 468 KB Output is correct
94 Correct 1 ms 468 KB Output is correct
95 Correct 1 ms 468 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 1 ms 340 KB Output is correct
98 Correct 1 ms 340 KB Output is correct
99 Correct 1 ms 212 KB Output is correct
100 Correct 235 ms 155108 KB Output is correct
101 Correct 251 ms 155180 KB Output is correct
102 Correct 231 ms 154996 KB Output is correct
103 Correct 243 ms 155148 KB Output is correct
104 Correct 232 ms 155248 KB Output is correct
105 Correct 250 ms 176104 KB Output is correct
106 Correct 269 ms 175992 KB Output is correct
107 Correct 253 ms 175976 KB Output is correct
108 Correct 258 ms 175892 KB Output is correct
109 Correct 247 ms 175860 KB Output is correct
110 Correct 249 ms 155096 KB Output is correct
111 Correct 243 ms 155048 KB Output is correct
112 Correct 231 ms 155252 KB Output is correct
113 Correct 238 ms 155000 KB Output is correct
114 Correct 233 ms 155120 KB Output is correct
115 Correct 297 ms 217840 KB Output is correct
116 Correct 282 ms 217728 KB Output is correct
117 Correct 301 ms 217732 KB Output is correct
118 Correct 275 ms 217740 KB Output is correct
119 Correct 282 ms 217724 KB Output is correct
120 Correct 237 ms 155044 KB Output is correct
121 Correct 228 ms 155112 KB Output is correct
122 Correct 254 ms 155004 KB Output is correct
123 Correct 249 ms 155116 KB Output is correct
124 Correct 268 ms 186500 KB Output is correct
125 Correct 236 ms 186420 KB Output is correct
126 Correct 243 ms 186312 KB Output is correct
127 Correct 249 ms 186412 KB Output is correct
128 Correct 133 ms 81356 KB Output is correct
129 Correct 138 ms 81456 KB Output is correct
130 Correct 130 ms 81408 KB Output is correct
131 Correct 0 ms 212 KB Output is correct