Submission #1088037

# Submission time Handle Problem Language Result Execution time Memory
1088037 2024-09-13T18:16:40 Z _8_8_ Homework (CEOI22_homework) C++17
0 / 100
164 ms 158908 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int  N = 2e6 + 12, MOD = 998244353;

string s;
int m, n, col, l[N], r[N];
vector<int> st, g[N];
bool ok[N];
void dfs(int v) {
    if(v <= col) return;
    for(int to:g[v]) {
        dfs(to);
    }
    if(ok[v]) {
        l[v] = max(l[g[v][0]], l[g[v][1]]);
        r[v] = max(r[g[v][0]], r[g[v][1]]);
        if(l[g[v][0]] == l[g[v][1]]) {
            l[v]++;
        }
    } else {
        r[v] = min(r[g[v][0]], r[g[v][1]]);
        if(r[g[v][0]] == r[g[v][1]]) {
            r[v]--;
        }
        l[v] = min(l[g[v][0]], l[g[v][1]]);
    }
    // cout << v << ' ' << l[v] << ' ' << r[v] << '\n';
}
void test() {
    cin >> s;
    m = (int)s.size();
    for(int i = 0; i < m; i++) {
        if(s[i] == '?') {
            col++;
        }
    }
    n = col;
    for(int i = 0; i < m; i++) {
        if(s[i] == '(') {
            if(!st.empty()) {
                g[st.back()].push_back(n + 1);
            }
            st.push_back(++n);
            if(s[i - 1] == 'x') {
                ok[n] = 1;
            } 
        } else if(s[i] == ')') {
            st.pop_back();
        }
    }
    int it = 0;
    // (()())
    for(int i = 1; i <= col; i++) {
        l[i] = 1,r[i] = col;
    }
    for(int i = col + 1; i <= n; i++) {
        int f = (int)g[i].size();
        for(int j = 0; j < 2 - f; j++) {
            g[i].push_back(++it);
        }
    }
    assert(it == col);
    dfs(col + 1);
    cout << r[col + 1] - l[col + 1] << '\n';
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); 

    int t = 1; 
    // cin >> t;

    while(t--) 
        test();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 47452 KB Output is correct
2 Incorrect 18 ms 47452 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 47452 KB Output is correct
2 Incorrect 18 ms 47452 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 164 ms 158908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 47452 KB Output is correct
2 Incorrect 18 ms 47452 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 47452 KB Output is correct
2 Incorrect 18 ms 47452 KB Output isn't correct
3 Halted 0 ms 0 KB -