답안 #784635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784635 2023-07-16T11:19:25 Z boris_mihov Homework (CEOI22_homework) C++17
53 / 100
62 ms 100636 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 = 2000000 + 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 22 ms 47316 KB Output is correct
2 Correct 23 ms 47288 KB Output is correct
3 Correct 22 ms 47308 KB Output is correct
4 Correct 26 ms 47320 KB Output is correct
5 Correct 30 ms 47272 KB Output is correct
6 Correct 22 ms 47332 KB Output is correct
7 Correct 23 ms 47236 KB Output is correct
8 Correct 23 ms 47316 KB Output is correct
9 Correct 25 ms 47296 KB Output is correct
10 Correct 22 ms 47316 KB Output is correct
11 Correct 22 ms 47232 KB Output is correct
12 Correct 22 ms 47236 KB Output is correct
13 Correct 27 ms 47316 KB Output is correct
14 Correct 27 ms 47292 KB Output is correct
15 Correct 23 ms 47312 KB Output is correct
16 Correct 23 ms 47320 KB Output is correct
17 Correct 23 ms 47316 KB Output is correct
18 Correct 23 ms 47348 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 23 ms 47328 KB Output is correct
21 Correct 23 ms 47352 KB Output is correct
22 Correct 23 ms 47344 KB Output is correct
23 Correct 23 ms 47332 KB Output is correct
24 Correct 23 ms 47240 KB Output is correct
25 Correct 24 ms 47308 KB Output is correct
26 Correct 23 ms 47264 KB Output is correct
27 Correct 24 ms 47344 KB Output is correct
28 Correct 23 ms 47308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47316 KB Output is correct
2 Correct 23 ms 47288 KB Output is correct
3 Correct 22 ms 47308 KB Output is correct
4 Correct 26 ms 47320 KB Output is correct
5 Correct 30 ms 47272 KB Output is correct
6 Correct 22 ms 47332 KB Output is correct
7 Correct 23 ms 47236 KB Output is correct
8 Correct 23 ms 47316 KB Output is correct
9 Correct 25 ms 47296 KB Output is correct
10 Correct 22 ms 47316 KB Output is correct
11 Correct 22 ms 47232 KB Output is correct
12 Correct 22 ms 47236 KB Output is correct
13 Correct 27 ms 47316 KB Output is correct
14 Correct 27 ms 47292 KB Output is correct
15 Correct 23 ms 47312 KB Output is correct
16 Correct 23 ms 47320 KB Output is correct
17 Correct 23 ms 47316 KB Output is correct
18 Correct 23 ms 47348 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 23 ms 47328 KB Output is correct
21 Correct 23 ms 47352 KB Output is correct
22 Correct 23 ms 47344 KB Output is correct
23 Correct 23 ms 47332 KB Output is correct
24 Correct 23 ms 47240 KB Output is correct
25 Correct 24 ms 47308 KB Output is correct
26 Correct 23 ms 47264 KB Output is correct
27 Correct 24 ms 47344 KB Output is correct
28 Correct 23 ms 47308 KB Output is correct
29 Correct 22 ms 47240 KB Output is correct
30 Correct 23 ms 47332 KB Output is correct
31 Correct 23 ms 47296 KB Output is correct
32 Correct 23 ms 47296 KB Output is correct
33 Correct 22 ms 47284 KB Output is correct
34 Correct 23 ms 47272 KB Output is correct
35 Correct 23 ms 47316 KB Output is correct
36 Correct 23 ms 47244 KB Output is correct
37 Correct 23 ms 47316 KB Output is correct
38 Correct 23 ms 47284 KB Output is correct
39 Correct 23 ms 47316 KB Output is correct
40 Correct 22 ms 47252 KB Output is correct
41 Correct 23 ms 47316 KB Output is correct
42 Correct 24 ms 47328 KB Output is correct
43 Correct 22 ms 47336 KB Output is correct
44 Correct 23 ms 47340 KB Output is correct
45 Correct 24 ms 47292 KB Output is correct
46 Correct 23 ms 47316 KB Output is correct
47 Correct 23 ms 47232 KB Output is correct
48 Correct 23 ms 47260 KB Output is correct
49 Correct 28 ms 47308 KB Output is correct
50 Correct 23 ms 47348 KB Output is correct
51 Correct 24 ms 47316 KB Output is correct
52 Correct 23 ms 47316 KB Output is correct
53 Correct 23 ms 47308 KB Output is correct
54 Correct 23 ms 47316 KB Output is correct
55 Correct 24 ms 47260 KB Output is correct
56 Correct 24 ms 47316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 62 ms 100636 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47316 KB Output is correct
2 Correct 23 ms 47288 KB Output is correct
3 Correct 22 ms 47308 KB Output is correct
4 Correct 26 ms 47320 KB Output is correct
5 Correct 30 ms 47272 KB Output is correct
6 Correct 22 ms 47332 KB Output is correct
7 Correct 23 ms 47236 KB Output is correct
8 Correct 23 ms 47316 KB Output is correct
9 Correct 25 ms 47296 KB Output is correct
10 Correct 22 ms 47316 KB Output is correct
11 Correct 22 ms 47232 KB Output is correct
12 Correct 22 ms 47236 KB Output is correct
13 Correct 27 ms 47316 KB Output is correct
14 Correct 27 ms 47292 KB Output is correct
15 Correct 23 ms 47312 KB Output is correct
16 Correct 23 ms 47320 KB Output is correct
17 Correct 23 ms 47316 KB Output is correct
18 Correct 23 ms 47348 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 23 ms 47328 KB Output is correct
21 Correct 23 ms 47352 KB Output is correct
22 Correct 23 ms 47344 KB Output is correct
23 Correct 23 ms 47332 KB Output is correct
24 Correct 23 ms 47240 KB Output is correct
25 Correct 24 ms 47308 KB Output is correct
26 Correct 23 ms 47264 KB Output is correct
27 Correct 24 ms 47344 KB Output is correct
28 Correct 23 ms 47308 KB Output is correct
29 Correct 22 ms 47240 KB Output is correct
30 Correct 23 ms 47332 KB Output is correct
31 Correct 23 ms 47296 KB Output is correct
32 Correct 23 ms 47296 KB Output is correct
33 Correct 22 ms 47284 KB Output is correct
34 Correct 23 ms 47272 KB Output is correct
35 Correct 23 ms 47316 KB Output is correct
36 Correct 23 ms 47244 KB Output is correct
37 Correct 23 ms 47316 KB Output is correct
38 Correct 23 ms 47284 KB Output is correct
39 Correct 23 ms 47316 KB Output is correct
40 Correct 22 ms 47252 KB Output is correct
41 Correct 23 ms 47316 KB Output is correct
42 Correct 24 ms 47328 KB Output is correct
43 Correct 22 ms 47336 KB Output is correct
44 Correct 23 ms 47340 KB Output is correct
45 Correct 24 ms 47292 KB Output is correct
46 Correct 23 ms 47316 KB Output is correct
47 Correct 23 ms 47232 KB Output is correct
48 Correct 23 ms 47260 KB Output is correct
49 Correct 28 ms 47308 KB Output is correct
50 Correct 23 ms 47348 KB Output is correct
51 Correct 24 ms 47316 KB Output is correct
52 Correct 23 ms 47316 KB Output is correct
53 Correct 23 ms 47308 KB Output is correct
54 Correct 23 ms 47316 KB Output is correct
55 Correct 24 ms 47260 KB Output is correct
56 Correct 24 ms 47316 KB Output is correct
57 Correct 27 ms 47444 KB Output is correct
58 Correct 23 ms 47488 KB Output is correct
59 Correct 28 ms 47436 KB Output is correct
60 Correct 28 ms 47388 KB Output is correct
61 Correct 25 ms 47500 KB Output is correct
62 Correct 23 ms 47492 KB Output is correct
63 Correct 23 ms 47520 KB Output is correct
64 Correct 28 ms 47428 KB Output is correct
65 Correct 28 ms 47528 KB Output is correct
66 Correct 23 ms 47444 KB Output is correct
67 Correct 24 ms 47488 KB Output is correct
68 Correct 28 ms 47492 KB Output is correct
69 Correct 28 ms 47444 KB Output is correct
70 Correct 23 ms 47504 KB Output is correct
71 Correct 23 ms 47500 KB Output is correct
72 Correct 23 ms 47444 KB Output is correct
73 Correct 23 ms 47472 KB Output is correct
74 Correct 23 ms 47504 KB Output is correct
75 Correct 23 ms 47444 KB Output is correct
76 Correct 25 ms 47476 KB Output is correct
77 Correct 23 ms 47388 KB Output is correct
78 Correct 29 ms 47464 KB Output is correct
79 Correct 24 ms 47452 KB Output is correct
80 Correct 23 ms 47424 KB Output is correct
81 Correct 22 ms 47432 KB Output is correct
82 Correct 29 ms 47496 KB Output is correct
83 Correct 23 ms 47352 KB Output is correct
84 Correct 23 ms 47340 KB Output is correct
85 Correct 24 ms 47408 KB Output is correct
86 Correct 23 ms 47220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 47316 KB Output is correct
2 Correct 23 ms 47288 KB Output is correct
3 Correct 22 ms 47308 KB Output is correct
4 Correct 26 ms 47320 KB Output is correct
5 Correct 30 ms 47272 KB Output is correct
6 Correct 22 ms 47332 KB Output is correct
7 Correct 23 ms 47236 KB Output is correct
8 Correct 23 ms 47316 KB Output is correct
9 Correct 25 ms 47296 KB Output is correct
10 Correct 22 ms 47316 KB Output is correct
11 Correct 22 ms 47232 KB Output is correct
12 Correct 22 ms 47236 KB Output is correct
13 Correct 27 ms 47316 KB Output is correct
14 Correct 27 ms 47292 KB Output is correct
15 Correct 23 ms 47312 KB Output is correct
16 Correct 23 ms 47320 KB Output is correct
17 Correct 23 ms 47316 KB Output is correct
18 Correct 23 ms 47348 KB Output is correct
19 Correct 24 ms 47292 KB Output is correct
20 Correct 23 ms 47328 KB Output is correct
21 Correct 23 ms 47352 KB Output is correct
22 Correct 23 ms 47344 KB Output is correct
23 Correct 23 ms 47332 KB Output is correct
24 Correct 23 ms 47240 KB Output is correct
25 Correct 24 ms 47308 KB Output is correct
26 Correct 23 ms 47264 KB Output is correct
27 Correct 24 ms 47344 KB Output is correct
28 Correct 23 ms 47308 KB Output is correct
29 Correct 22 ms 47240 KB Output is correct
30 Correct 23 ms 47332 KB Output is correct
31 Correct 23 ms 47296 KB Output is correct
32 Correct 23 ms 47296 KB Output is correct
33 Correct 22 ms 47284 KB Output is correct
34 Correct 23 ms 47272 KB Output is correct
35 Correct 23 ms 47316 KB Output is correct
36 Correct 23 ms 47244 KB Output is correct
37 Correct 23 ms 47316 KB Output is correct
38 Correct 23 ms 47284 KB Output is correct
39 Correct 23 ms 47316 KB Output is correct
40 Correct 22 ms 47252 KB Output is correct
41 Correct 23 ms 47316 KB Output is correct
42 Correct 24 ms 47328 KB Output is correct
43 Correct 22 ms 47336 KB Output is correct
44 Correct 23 ms 47340 KB Output is correct
45 Correct 24 ms 47292 KB Output is correct
46 Correct 23 ms 47316 KB Output is correct
47 Correct 23 ms 47232 KB Output is correct
48 Correct 23 ms 47260 KB Output is correct
49 Correct 28 ms 47308 KB Output is correct
50 Correct 23 ms 47348 KB Output is correct
51 Correct 24 ms 47316 KB Output is correct
52 Correct 23 ms 47316 KB Output is correct
53 Correct 23 ms 47308 KB Output is correct
54 Correct 23 ms 47316 KB Output is correct
55 Correct 24 ms 47260 KB Output is correct
56 Correct 24 ms 47316 KB Output is correct
57 Runtime error 62 ms 100636 KB Execution killed with signal 11
58 Halted 0 ms 0 KB -