답안 #784636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784636 2023-07-16T11:19:45 Z boris_mihov Homework (CEOI22_homework) C++17
100 / 100
526 ms 447612 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXLEN = 10000000;
const int MAXN = 10000000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;

char s[MAXN];
int type[MAXN];
int dpMIN[MAXN][3];
int dpMAX[MAXN][3];
bool blMIN[MAXN][3];
bool blMAX[MAXN][3];
std::vector <int> g[MAXN];
std::stack <int> st;
int next[MAXN];
int end[MAXN];
int cnt;
int n;

int buildTree(int l, int r)
{
    if (l == r)
    {
        return ++cnt;
    }

    int node = ++cnt;
    if (s[l + 1] == 'i') type[node] = 1;
    else if (s[l + 1] == 'a') type[node] = 2;
    else assert(false);

    g[node].push_back(buildTree(l + 4, next[l + 3] - 1));
    g[node].push_back(buildTree(next[l + 3] + 1, end[l + 3] - 1));
    return node;
}

int fMIN(int node, int asked)
{
    if (g[node].empty())
    {
        if (asked == 0) return 1;
        return 0;
    }

    if (blMIN[node][asked])
    {
        return dpMIN[node][asked];
    }

    blMIN[node][asked] = true;
    dpMIN[node][asked] = INTINF;
    if (asked == 0)
    {
        if (type[node] == 1)
        {
            dpMIN[node][asked] = std::min(fMIN(g[node][0], 0), fMIN(g[node][1], 0));
        } else
        {
            dpMIN[node][asked] = fMIN(g[node][0], 0) + fMIN(g[node][1], 0);
        }
    } else if (asked == 1)
    {
        if (type[node] == 1)
        {
            dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 1) + fMIN(g[node][1], 2));
            dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 2) + fMIN(g[node][1], 1));
        } else
        {
            dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 1) + fMIN(g[node][1], 0));
            dpMIN[node][asked] = std::min(dpMIN[node][asked], fMIN(g[node][0], 0) + fMIN(g[node][1], 1));
        }
    } else
    {
        if (type[node] == 2)
        {
            dpMIN[node][asked] = std::min(fMIN(g[node][0], 2), fMIN(g[node][1], 2));
        } else
        {
            dpMIN[node][asked] = fMIN(g[node][0], 2) + fMIN(g[node][1], 2);
        }
    }

    return dpMIN[node][asked];
}

int fMAX(int node, int asked)
{
    if (g[node].empty())
    {
        if (asked == 0) return 1;
        return 0;
    }

    if (blMAX[node][asked])
    {
        return dpMAX[node][asked];
    }

    blMAX[node][asked] = true;
    dpMAX[node][asked] = -INTINF;
    if (asked == 0)
    {
        if (type[node] == 1)
        {
            dpMAX[node][asked] = std::max(fMAX(g[node][0], 0) + std::max(fMAX(g[node][1], 0), fMAX(g[node][1], 2)), std::max(fMAX(g[node][0], 0), fMAX(g[node][0], 2)) + fMAX(g[node][1], 0));
        } else
        {
            dpMAX[node][asked] = fMAX(g[node][0], 0) + fMAX(g[node][1], 0);
        }
    } else if (asked == 1)
    {
        if (type[node] == 1)
        {
            dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 1) + fMAX(g[node][1], 2));
            dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 2) + fMAX(g[node][1], 1));
        } else
        {
            dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 1) + fMAX(g[node][1], 0));
            dpMAX[node][asked] = std::max(dpMAX[node][asked], fMAX(g[node][0], 0) + fMAX(g[node][1], 1));
        }
    } else
    {
        if (type[node] == 2)
        {
            dpMAX[node][asked] = std::max(fMAX(g[node][0], 2) + std::max(fMAX(g[node][1], 0), fMAX(g[node][1], 2)), std::max(fMAX(g[node][0], 0), fMAX(g[node][0], 2)) + fMAX(g[node][1], 2));
        } else
        {
            dpMAX[node][asked] = fMAX(g[node][0], 2) + fMAX(g[node][1], 2);
        }
    }

    return dpMAX[node][asked];
}

void solve()
{
    int len = strlen(s + 1);
    for (int i = 1 ; i <= len ; ++i)
    {
        if (s[i] == '(') st.push(i);
        if (s[i] == ',')
        {
            next[st.top()] = i;
            st.pop();
        }
    }

    while (!st.empty())
    {
        st.pop();
    }

    for (int i = 1 ; i <= len ; ++i)
    {
        if (s[i] == '(') st.push(i);
        if (s[i] == ')')
        {
            end[st.top()] = i;
            st.pop();
        }
    }

    buildTree(1, len);
    n = cnt;

    // std::cout << "type\n";
    // for (int i = 1 ; i <= n ; ++i)
    // {
    //     std::cout << type[i] << '\n';
    // }

    // std::cout << "tree\n";
    // for (int i = 1 ; i <= n ; ++i)
    // {
    //     for (const int &u : g[i])
    //     {
    //         std::cout << i << ' ' << u << '\n';
    //     }
    // }

    std::cout << fMAX(1, 1) - fMIN(1, 1) + 1 << '\n';
}

void input()
{
    std::cin >> s + 1;
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

Compilation message

Main.cpp: In function 'void input()':
Main.cpp:194:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  194 |     std::cin >> s + 1;
      |                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 235172 KB Output is correct
2 Correct 104 ms 235168 KB Output is correct
3 Correct 102 ms 235124 KB Output is correct
4 Correct 104 ms 235132 KB Output is correct
5 Correct 103 ms 235092 KB Output is correct
6 Correct 103 ms 235200 KB Output is correct
7 Correct 105 ms 235084 KB Output is correct
8 Correct 104 ms 235100 KB Output is correct
9 Correct 103 ms 235084 KB Output is correct
10 Correct 108 ms 235096 KB Output is correct
11 Correct 104 ms 235140 KB Output is correct
12 Correct 109 ms 235112 KB Output is correct
13 Correct 114 ms 235168 KB Output is correct
14 Correct 105 ms 235080 KB Output is correct
15 Correct 103 ms 235184 KB Output is correct
16 Correct 103 ms 235112 KB Output is correct
17 Correct 103 ms 235200 KB Output is correct
18 Correct 104 ms 235172 KB Output is correct
19 Correct 103 ms 235200 KB Output is correct
20 Correct 103 ms 235084 KB Output is correct
21 Correct 104 ms 235148 KB Output is correct
22 Correct 103 ms 235104 KB Output is correct
23 Correct 111 ms 235184 KB Output is correct
24 Correct 104 ms 235108 KB Output is correct
25 Correct 103 ms 235196 KB Output is correct
26 Correct 104 ms 235116 KB Output is correct
27 Correct 103 ms 235124 KB Output is correct
28 Correct 103 ms 235136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 235172 KB Output is correct
2 Correct 104 ms 235168 KB Output is correct
3 Correct 102 ms 235124 KB Output is correct
4 Correct 104 ms 235132 KB Output is correct
5 Correct 103 ms 235092 KB Output is correct
6 Correct 103 ms 235200 KB Output is correct
7 Correct 105 ms 235084 KB Output is correct
8 Correct 104 ms 235100 KB Output is correct
9 Correct 103 ms 235084 KB Output is correct
10 Correct 108 ms 235096 KB Output is correct
11 Correct 104 ms 235140 KB Output is correct
12 Correct 109 ms 235112 KB Output is correct
13 Correct 114 ms 235168 KB Output is correct
14 Correct 105 ms 235080 KB Output is correct
15 Correct 103 ms 235184 KB Output is correct
16 Correct 103 ms 235112 KB Output is correct
17 Correct 103 ms 235200 KB Output is correct
18 Correct 104 ms 235172 KB Output is correct
19 Correct 103 ms 235200 KB Output is correct
20 Correct 103 ms 235084 KB Output is correct
21 Correct 104 ms 235148 KB Output is correct
22 Correct 103 ms 235104 KB Output is correct
23 Correct 111 ms 235184 KB Output is correct
24 Correct 104 ms 235108 KB Output is correct
25 Correct 103 ms 235196 KB Output is correct
26 Correct 104 ms 235116 KB Output is correct
27 Correct 103 ms 235124 KB Output is correct
28 Correct 103 ms 235136 KB Output is correct
29 Correct 104 ms 235192 KB Output is correct
30 Correct 103 ms 235084 KB Output is correct
31 Correct 108 ms 235092 KB Output is correct
32 Correct 105 ms 235116 KB Output is correct
33 Correct 104 ms 235184 KB Output is correct
34 Correct 119 ms 235188 KB Output is correct
35 Correct 105 ms 235196 KB Output is correct
36 Correct 104 ms 235088 KB Output is correct
37 Correct 106 ms 235224 KB Output is correct
38 Correct 108 ms 235144 KB Output is correct
39 Correct 119 ms 235152 KB Output is correct
40 Correct 104 ms 235152 KB Output is correct
41 Correct 104 ms 235164 KB Output is correct
42 Correct 104 ms 235084 KB Output is correct
43 Correct 105 ms 235080 KB Output is correct
44 Correct 105 ms 235104 KB Output is correct
45 Correct 104 ms 235168 KB Output is correct
46 Correct 109 ms 235076 KB Output is correct
47 Correct 119 ms 235124 KB Output is correct
48 Correct 103 ms 235132 KB Output is correct
49 Correct 103 ms 235160 KB Output is correct
50 Correct 104 ms 235076 KB Output is correct
51 Correct 105 ms 235100 KB Output is correct
52 Correct 111 ms 235312 KB Output is correct
53 Correct 105 ms 235196 KB Output is correct
54 Correct 105 ms 235092 KB Output is correct
55 Correct 103 ms 235136 KB Output is correct
56 Correct 105 ms 235120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 453 ms 445372 KB Output is correct
2 Correct 451 ms 447556 KB Output is correct
3 Correct 500 ms 447576 KB Output is correct
4 Correct 516 ms 447548 KB Output is correct
5 Correct 500 ms 447548 KB Output is correct
6 Correct 508 ms 447564 KB Output is correct
7 Correct 526 ms 447548 KB Output is correct
8 Correct 502 ms 447588 KB Output is correct
9 Correct 499 ms 447596 KB Output is correct
10 Correct 505 ms 447612 KB Output is correct
11 Correct 514 ms 447600 KB Output is correct
12 Correct 485 ms 447588 KB Output is correct
13 Correct 115 ms 235084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 235172 KB Output is correct
2 Correct 104 ms 235168 KB Output is correct
3 Correct 102 ms 235124 KB Output is correct
4 Correct 104 ms 235132 KB Output is correct
5 Correct 103 ms 235092 KB Output is correct
6 Correct 103 ms 235200 KB Output is correct
7 Correct 105 ms 235084 KB Output is correct
8 Correct 104 ms 235100 KB Output is correct
9 Correct 103 ms 235084 KB Output is correct
10 Correct 108 ms 235096 KB Output is correct
11 Correct 104 ms 235140 KB Output is correct
12 Correct 109 ms 235112 KB Output is correct
13 Correct 114 ms 235168 KB Output is correct
14 Correct 105 ms 235080 KB Output is correct
15 Correct 103 ms 235184 KB Output is correct
16 Correct 103 ms 235112 KB Output is correct
17 Correct 103 ms 235200 KB Output is correct
18 Correct 104 ms 235172 KB Output is correct
19 Correct 103 ms 235200 KB Output is correct
20 Correct 103 ms 235084 KB Output is correct
21 Correct 104 ms 235148 KB Output is correct
22 Correct 103 ms 235104 KB Output is correct
23 Correct 111 ms 235184 KB Output is correct
24 Correct 104 ms 235108 KB Output is correct
25 Correct 103 ms 235196 KB Output is correct
26 Correct 104 ms 235116 KB Output is correct
27 Correct 103 ms 235124 KB Output is correct
28 Correct 103 ms 235136 KB Output is correct
29 Correct 104 ms 235192 KB Output is correct
30 Correct 103 ms 235084 KB Output is correct
31 Correct 108 ms 235092 KB Output is correct
32 Correct 105 ms 235116 KB Output is correct
33 Correct 104 ms 235184 KB Output is correct
34 Correct 119 ms 235188 KB Output is correct
35 Correct 105 ms 235196 KB Output is correct
36 Correct 104 ms 235088 KB Output is correct
37 Correct 106 ms 235224 KB Output is correct
38 Correct 108 ms 235144 KB Output is correct
39 Correct 119 ms 235152 KB Output is correct
40 Correct 104 ms 235152 KB Output is correct
41 Correct 104 ms 235164 KB Output is correct
42 Correct 104 ms 235084 KB Output is correct
43 Correct 105 ms 235080 KB Output is correct
44 Correct 105 ms 235104 KB Output is correct
45 Correct 104 ms 235168 KB Output is correct
46 Correct 109 ms 235076 KB Output is correct
47 Correct 119 ms 235124 KB Output is correct
48 Correct 103 ms 235132 KB Output is correct
49 Correct 103 ms 235160 KB Output is correct
50 Correct 104 ms 235076 KB Output is correct
51 Correct 105 ms 235100 KB Output is correct
52 Correct 111 ms 235312 KB Output is correct
53 Correct 105 ms 235196 KB Output is correct
54 Correct 105 ms 235092 KB Output is correct
55 Correct 103 ms 235136 KB Output is correct
56 Correct 105 ms 235120 KB Output is correct
57 Correct 106 ms 235360 KB Output is correct
58 Correct 105 ms 235356 KB Output is correct
59 Correct 107 ms 235356 KB Output is correct
60 Correct 105 ms 235276 KB Output is correct
61 Correct 106 ms 235344 KB Output is correct
62 Correct 107 ms 235304 KB Output is correct
63 Correct 108 ms 235296 KB Output is correct
64 Correct 107 ms 235372 KB Output is correct
65 Correct 107 ms 235388 KB Output is correct
66 Correct 110 ms 235292 KB Output is correct
67 Correct 105 ms 235368 KB Output is correct
68 Correct 106 ms 235328 KB Output is correct
69 Correct 106 ms 235308 KB Output is correct
70 Correct 114 ms 235344 KB Output is correct
71 Correct 118 ms 235396 KB Output is correct
72 Correct 110 ms 235412 KB Output is correct
73 Correct 110 ms 235404 KB Output is correct
74 Correct 106 ms 235340 KB Output is correct
75 Correct 104 ms 235328 KB Output is correct
76 Correct 113 ms 235396 KB Output is correct
77 Correct 107 ms 235360 KB Output is correct
78 Correct 115 ms 235280 KB Output is correct
79 Correct 106 ms 235368 KB Output is correct
80 Correct 111 ms 235360 KB Output is correct
81 Correct 121 ms 235296 KB Output is correct
82 Correct 119 ms 235368 KB Output is correct
83 Correct 107 ms 235232 KB Output is correct
84 Correct 105 ms 235184 KB Output is correct
85 Correct 108 ms 235212 KB Output is correct
86 Correct 106 ms 235196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 235172 KB Output is correct
2 Correct 104 ms 235168 KB Output is correct
3 Correct 102 ms 235124 KB Output is correct
4 Correct 104 ms 235132 KB Output is correct
5 Correct 103 ms 235092 KB Output is correct
6 Correct 103 ms 235200 KB Output is correct
7 Correct 105 ms 235084 KB Output is correct
8 Correct 104 ms 235100 KB Output is correct
9 Correct 103 ms 235084 KB Output is correct
10 Correct 108 ms 235096 KB Output is correct
11 Correct 104 ms 235140 KB Output is correct
12 Correct 109 ms 235112 KB Output is correct
13 Correct 114 ms 235168 KB Output is correct
14 Correct 105 ms 235080 KB Output is correct
15 Correct 103 ms 235184 KB Output is correct
16 Correct 103 ms 235112 KB Output is correct
17 Correct 103 ms 235200 KB Output is correct
18 Correct 104 ms 235172 KB Output is correct
19 Correct 103 ms 235200 KB Output is correct
20 Correct 103 ms 235084 KB Output is correct
21 Correct 104 ms 235148 KB Output is correct
22 Correct 103 ms 235104 KB Output is correct
23 Correct 111 ms 235184 KB Output is correct
24 Correct 104 ms 235108 KB Output is correct
25 Correct 103 ms 235196 KB Output is correct
26 Correct 104 ms 235116 KB Output is correct
27 Correct 103 ms 235124 KB Output is correct
28 Correct 103 ms 235136 KB Output is correct
29 Correct 104 ms 235192 KB Output is correct
30 Correct 103 ms 235084 KB Output is correct
31 Correct 108 ms 235092 KB Output is correct
32 Correct 105 ms 235116 KB Output is correct
33 Correct 104 ms 235184 KB Output is correct
34 Correct 119 ms 235188 KB Output is correct
35 Correct 105 ms 235196 KB Output is correct
36 Correct 104 ms 235088 KB Output is correct
37 Correct 106 ms 235224 KB Output is correct
38 Correct 108 ms 235144 KB Output is correct
39 Correct 119 ms 235152 KB Output is correct
40 Correct 104 ms 235152 KB Output is correct
41 Correct 104 ms 235164 KB Output is correct
42 Correct 104 ms 235084 KB Output is correct
43 Correct 105 ms 235080 KB Output is correct
44 Correct 105 ms 235104 KB Output is correct
45 Correct 104 ms 235168 KB Output is correct
46 Correct 109 ms 235076 KB Output is correct
47 Correct 119 ms 235124 KB Output is correct
48 Correct 103 ms 235132 KB Output is correct
49 Correct 103 ms 235160 KB Output is correct
50 Correct 104 ms 235076 KB Output is correct
51 Correct 105 ms 235100 KB Output is correct
52 Correct 111 ms 235312 KB Output is correct
53 Correct 105 ms 235196 KB Output is correct
54 Correct 105 ms 235092 KB Output is correct
55 Correct 103 ms 235136 KB Output is correct
56 Correct 105 ms 235120 KB Output is correct
57 Correct 453 ms 445372 KB Output is correct
58 Correct 451 ms 447556 KB Output is correct
59 Correct 500 ms 447576 KB Output is correct
60 Correct 516 ms 447548 KB Output is correct
61 Correct 500 ms 447548 KB Output is correct
62 Correct 508 ms 447564 KB Output is correct
63 Correct 526 ms 447548 KB Output is correct
64 Correct 502 ms 447588 KB Output is correct
65 Correct 499 ms 447596 KB Output is correct
66 Correct 505 ms 447612 KB Output is correct
67 Correct 514 ms 447600 KB Output is correct
68 Correct 485 ms 447588 KB Output is correct
69 Correct 115 ms 235084 KB Output is correct
70 Correct 106 ms 235360 KB Output is correct
71 Correct 105 ms 235356 KB Output is correct
72 Correct 107 ms 235356 KB Output is correct
73 Correct 105 ms 235276 KB Output is correct
74 Correct 106 ms 235344 KB Output is correct
75 Correct 107 ms 235304 KB Output is correct
76 Correct 108 ms 235296 KB Output is correct
77 Correct 107 ms 235372 KB Output is correct
78 Correct 107 ms 235388 KB Output is correct
79 Correct 110 ms 235292 KB Output is correct
80 Correct 105 ms 235368 KB Output is correct
81 Correct 106 ms 235328 KB Output is correct
82 Correct 106 ms 235308 KB Output is correct
83 Correct 114 ms 235344 KB Output is correct
84 Correct 118 ms 235396 KB Output is correct
85 Correct 110 ms 235412 KB Output is correct
86 Correct 110 ms 235404 KB Output is correct
87 Correct 106 ms 235340 KB Output is correct
88 Correct 104 ms 235328 KB Output is correct
89 Correct 113 ms 235396 KB Output is correct
90 Correct 107 ms 235360 KB Output is correct
91 Correct 115 ms 235280 KB Output is correct
92 Correct 106 ms 235368 KB Output is correct
93 Correct 111 ms 235360 KB Output is correct
94 Correct 121 ms 235296 KB Output is correct
95 Correct 119 ms 235368 KB Output is correct
96 Correct 107 ms 235232 KB Output is correct
97 Correct 105 ms 235184 KB Output is correct
98 Correct 108 ms 235212 KB Output is correct
99 Correct 106 ms 235196 KB Output is correct
100 Correct 492 ms 401500 KB Output is correct
101 Correct 479 ms 401532 KB Output is correct
102 Correct 469 ms 401484 KB Output is correct
103 Correct 439 ms 401532 KB Output is correct
104 Correct 458 ms 401528 KB Output is correct
105 Correct 494 ms 427620 KB Output is correct
106 Correct 489 ms 427608 KB Output is correct
107 Correct 501 ms 427656 KB Output is correct
108 Correct 474 ms 427512 KB Output is correct
109 Correct 489 ms 427656 KB Output is correct
110 Correct 486 ms 401464 KB Output is correct
111 Correct 488 ms 401488 KB Output is correct
112 Correct 474 ms 401624 KB Output is correct
113 Correct 468 ms 401644 KB Output is correct
114 Correct 477 ms 401460 KB Output is correct
115 Correct 520 ms 447564 KB Output is correct
116 Correct 525 ms 447484 KB Output is correct
117 Correct 516 ms 447512 KB Output is correct
118 Correct 525 ms 447548 KB Output is correct
119 Correct 511 ms 447524 KB Output is correct
120 Correct 447 ms 401524 KB Output is correct
121 Correct 419 ms 401648 KB Output is correct
122 Correct 486 ms 401532 KB Output is correct
123 Correct 500 ms 401544 KB Output is correct
124 Correct 456 ms 436864 KB Output is correct
125 Correct 455 ms 436740 KB Output is correct
126 Correct 484 ms 436792 KB Output is correct
127 Correct 463 ms 436732 KB Output is correct
128 Correct 273 ms 322400 KB Output is correct
129 Correct 270 ms 322404 KB Output is correct
130 Correct 282 ms 322432 KB Output is correct
131 Correct 108 ms 235152 KB Output is correct