답안 #763763

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
763763 2023-06-22T20:23:26 Z swagchicken Homework (CEOI22_homework) C++14
100 / 100
80 ms 84768 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
 
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
 
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
 
template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
 
const ll MOD  =  1000000007;
#define inf 1e18;
#define INF INT_MAX;
 
long double PI = 4*atan(1);
long double eps = 1e-8;

struct node {
    int a=0,b=0,m=0,t=1;
};

node tree[3000000];
int lft[3000000], rgt[3000000] = {};
string s; 


void parse() {
    int n = s.length();
    stack<int> stk;
    int curr = 1;

    for(int cp = 0; cp < n;) {
        if(s[cp] == 'm') {
            int t = -1;
            if(s[cp+1] == 'a') t = 1;
            
            if(!stk.empty()) {
                if(lft[stk.top()] == 0) 
                    lft[stk.top()] = curr;
                else 
                    rgt[stk.top()] = curr; 
            }
            tree[curr].t = t;
            stk.push(curr);
            curr++;
            cp += 4;
        }
        else if(s[cp] == ')') {
            stk.pop(); cp++;
        }
        else if(s[cp] == '?') {
            if(lft[stk.top()] == 0) 
                lft[stk.top()] = curr;
            else 
                rgt[stk.top()] = curr; 
            curr++;
            cp++;
        } else {
            cp++;
        }
    }
}

void dfs(int idx) {
    int L = lft[idx]; 
    int R = rgt[idx];
    if(L == 0 && R == 0) return;

    dfs(L); dfs(R);
    tree[idx].m = tree[L].m + tree[R].m + 1;
    if(tree[idx].t == 1) {
        tree[idx].a = tree[L].a + tree[R].a + 1;
        tree[idx].b = max(tree[L].m + tree[R].b + 1, tree[R].m + tree[L].b + 1);
    } else {
        tree[idx].a = min(tree[L].a, tree[R].a);
        tree[idx].b = tree[L].b + tree[R].b;
    }
}   

int main() {
    //auto start = chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);cin.tie(0);
    // freopen("promote.in", "r", stdin);
    // freopen("promote.out", "w", stdout);
    #ifdef DEBUG
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif 

    cin >> s;
    parse();
    dfs(1);
    cout << tree[1].b - tree[1].a + 1 << endl;
    
    
    

    





    

    
    // auto stop = chrono::high_resolution_clock::now();
    // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
    // cout << duration.count() << endl;
    //cin.close();
    //cout.close();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 464 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 77896 KB Output is correct
2 Correct 77 ms 84656 KB Output is correct
3 Correct 77 ms 84656 KB Output is correct
4 Correct 80 ms 84740 KB Output is correct
5 Correct 73 ms 84660 KB Output is correct
6 Correct 79 ms 84648 KB Output is correct
7 Correct 79 ms 84768 KB Output is correct
8 Correct 75 ms 84664 KB Output is correct
9 Correct 75 ms 84584 KB Output is correct
10 Correct 77 ms 84712 KB Output is correct
11 Correct 75 ms 84676 KB Output is correct
12 Correct 75 ms 84648 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 464 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 0 ms 340 KB Output is correct
58 Correct 0 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 340 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 0 ms 340 KB Output is correct
64 Correct 0 ms 340 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 0 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 0 ms 340 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 0 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 0 ms 340 KB Output is correct
74 Correct 1 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 1 ms 464 KB Output is correct
77 Correct 0 ms 340 KB Output is correct
78 Correct 0 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 1 ms 340 KB Output is correct
81 Correct 0 ms 340 KB Output is correct
82 Correct 0 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 0 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 464 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 77 ms 77896 KB Output is correct
58 Correct 77 ms 84656 KB Output is correct
59 Correct 77 ms 84656 KB Output is correct
60 Correct 80 ms 84740 KB Output is correct
61 Correct 73 ms 84660 KB Output is correct
62 Correct 79 ms 84648 KB Output is correct
63 Correct 79 ms 84768 KB Output is correct
64 Correct 75 ms 84664 KB Output is correct
65 Correct 75 ms 84584 KB Output is correct
66 Correct 77 ms 84712 KB Output is correct
67 Correct 75 ms 84676 KB Output is correct
68 Correct 75 ms 84648 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 0 ms 340 KB Output is correct
71 Correct 0 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 1 ms 340 KB Output is correct
74 Correct 1 ms 340 KB Output is correct
75 Correct 1 ms 340 KB Output is correct
76 Correct 0 ms 340 KB Output is correct
77 Correct 0 ms 340 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 0 ms 340 KB Output is correct
80 Correct 1 ms 340 KB Output is correct
81 Correct 0 ms 340 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 0 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 0 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 1 ms 340 KB Output is correct
89 Correct 1 ms 464 KB Output is correct
90 Correct 0 ms 340 KB Output is correct
91 Correct 0 ms 340 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 340 KB Output is correct
94 Correct 0 ms 340 KB Output is correct
95 Correct 0 ms 340 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 0 ms 340 KB Output is correct
98 Correct 1 ms 340 KB Output is correct
99 Correct 0 ms 340 KB Output is correct
100 Correct 66 ms 61100 KB Output is correct
101 Correct 60 ms 61076 KB Output is correct
102 Correct 66 ms 61108 KB Output is correct
103 Correct 56 ms 60996 KB Output is correct
104 Correct 56 ms 61112 KB Output is correct
105 Correct 70 ms 72796 KB Output is correct
106 Correct 75 ms 72732 KB Output is correct
107 Correct 68 ms 72752 KB Output is correct
108 Correct 69 ms 72788 KB Output is correct
109 Correct 66 ms 72776 KB Output is correct
110 Correct 64 ms 61120 KB Output is correct
111 Correct 65 ms 61180 KB Output is correct
112 Correct 61 ms 61108 KB Output is correct
113 Correct 65 ms 61108 KB Output is correct
114 Correct 60 ms 60996 KB Output is correct
115 Correct 78 ms 84652 KB Output is correct
116 Correct 78 ms 84548 KB Output is correct
117 Correct 78 ms 84684 KB Output is correct
118 Correct 79 ms 84656 KB Output is correct
119 Correct 73 ms 84660 KB Output is correct
120 Correct 59 ms 61084 KB Output is correct
121 Correct 63 ms 61096 KB Output is correct
122 Correct 63 ms 61000 KB Output is correct
123 Correct 64 ms 61092 KB Output is correct
124 Correct 74 ms 78648 KB Output is correct
125 Correct 63 ms 78628 KB Output is correct
126 Correct 62 ms 78660 KB Output is correct
127 Correct 60 ms 78644 KB Output is correct
128 Correct 26 ms 32188 KB Output is correct
129 Correct 25 ms 32208 KB Output is correct
130 Correct 23 ms 32212 KB Output is correct
131 Correct 1 ms 336 KB Output is correct