답안 #639526

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
639526 2022-09-10T12:23:13 Z MilosMilutinovic Homework (CEOI22_homework) C++14
100 / 100
299 ms 233120 KB
/**
 *    author:  wxhtzdy
 *    created: 07.09.2022 19:09:46
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
string to_string(const char* s) {
  return to_string((string) s);
}
string to_string(bool b) {
  return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  string s;
  cin >> s;
  int len = (int) s.size();
  vector<int> stk;
  vector<int> mat(len);
  for (int i = 0; i < len; i++) {
    if (s[i] == '(') {
      stk.push_back(i); 
    }
    if (s[i] == ')') {
      mat[stk.back()] = i;
      stk.pop_back();
    }
  }     
  int n = 0;
  for (int i = 0; i < len; i++) {
    if (s[i] == '?') {
      n += 1;
    }
  }
  vector<vector<int>> g(2 * n);
  vector<int> type(2 * n);
  int T = 0;
  function<void(int, int, int)> Solve = [&](int L, int R, int idx) {
    if (L == R) {
      return;
    }
    if (s[L + 1] == 'i') {
      type[idx] = 0;
    } else {
      type[idx] = 1;
    }
    T++;
    g[idx].push_back(T);
    L = L + 4;  
    if (s[L] != '?') {
      Solve(L, mat[L + 3], T);
      L = mat[L + 3] + 2;
    } else {
      L += 2;
    }
    T++;
    g[idx].push_back(T);
    if (s[L] != '?') {
      Solve(L, R - 1, T);  
    }
  };
  Solve(0, len - 1, 0);
  vector<int> L(2 * n);
  vector<int> R(2 * n);
  vector<int> sz(2 * n);
  function<void(int)> Dfs = [&](int v) {
    if (g[v].empty()) {
      L[v] = R[v] = sz[v] = 1;
      return;
    }
    assert((int) g[v].size() == 2);
    for (int u : g[v]) {
      Dfs(u);        
    }           
    sz[v] = sz[g[v][0]] + sz[g[v][1]];
    if (type[v] == 0) {
      L[v] = min(L[g[v][0]], L[g[v][1]]);    
      R[v] = R[g[v][0]] + R[g[v][1]] - 1;
    } else {
      L[v] = L[g[v][0]] + L[g[v][1]];
      R[v] = max(R[g[v][0]] + sz[g[v][1]], sz[g[v][0]] + R[g[v][1]]);
    }
  };
  Dfs(0);
  cout << R[0] - L[0] + 1 << '\n';                                               
  return 0;
}                                    
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 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 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 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 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 316 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 324 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 0 ms 212 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 324 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 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 283 ms 231204 KB Output is correct
2 Correct 299 ms 231748 KB Output is correct
3 Correct 287 ms 231260 KB Output is correct
4 Correct 281 ms 232968 KB Output is correct
5 Correct 291 ms 232736 KB Output is correct
6 Correct 284 ms 232932 KB Output is correct
7 Correct 282 ms 232148 KB Output is correct
8 Correct 282 ms 232392 KB Output is correct
9 Correct 281 ms 231748 KB Output is correct
10 Correct 284 ms 231192 KB Output is correct
11 Correct 283 ms 233120 KB Output is correct
12 Correct 283 ms 231648 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 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 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 316 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 324 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 0 ms 212 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 324 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 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 1 ms 468 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 468 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 448 KB Output is correct
68 Correct 1 ms 456 KB Output is correct
69 Correct 1 ms 468 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 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 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 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 316 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 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 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 316 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 324 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 0 ms 212 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 324 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 212 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 212 KB Output is correct
57 Correct 283 ms 231204 KB Output is correct
58 Correct 299 ms 231748 KB Output is correct
59 Correct 287 ms 231260 KB Output is correct
60 Correct 281 ms 232968 KB Output is correct
61 Correct 291 ms 232736 KB Output is correct
62 Correct 284 ms 232932 KB Output is correct
63 Correct 282 ms 232148 KB Output is correct
64 Correct 282 ms 232392 KB Output is correct
65 Correct 281 ms 231748 KB Output is correct
66 Correct 284 ms 231192 KB Output is correct
67 Correct 283 ms 233120 KB Output is correct
68 Correct 283 ms 231648 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
70 Correct 1 ms 468 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 468 KB Output is correct
73 Correct 1 ms 340 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 468 KB Output is correct
79 Correct 1 ms 468 KB Output is correct
80 Correct 1 ms 448 KB Output is correct
81 Correct 1 ms 456 KB Output is correct
82 Correct 1 ms 468 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 468 KB Output is correct
92 Correct 1 ms 468 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 0 ms 212 KB Output is correct
100 Correct 242 ms 150684 KB Output is correct
101 Correct 237 ms 149892 KB Output is correct
102 Correct 228 ms 149772 KB Output is correct
103 Correct 227 ms 150012 KB Output is correct
104 Correct 228 ms 148952 KB Output is correct
105 Correct 264 ms 176796 KB Output is correct
106 Correct 284 ms 176584 KB Output is correct
107 Correct 274 ms 176776 KB Output is correct
108 Correct 280 ms 177508 KB Output is correct
109 Correct 280 ms 177968 KB Output is correct
110 Correct 234 ms 150924 KB Output is correct
111 Correct 240 ms 149804 KB Output is correct
112 Correct 227 ms 150296 KB Output is correct
113 Correct 243 ms 149744 KB Output is correct
114 Correct 236 ms 150376 KB Output is correct
115 Correct 296 ms 232912 KB Output is correct
116 Correct 290 ms 231996 KB Output is correct
117 Correct 286 ms 232888 KB Output is correct
118 Correct 279 ms 231920 KB Output is correct
119 Correct 293 ms 231836 KB Output is correct
120 Correct 247 ms 150436 KB Output is correct
121 Correct 232 ms 150568 KB Output is correct
122 Correct 275 ms 150340 KB Output is correct
123 Correct 243 ms 150416 KB Output is correct
124 Correct 260 ms 191976 KB Output is correct
125 Correct 262 ms 191752 KB Output is correct
126 Correct 251 ms 191188 KB Output is correct
127 Correct 277 ms 191924 KB Output is correct
128 Correct 115 ms 79148 KB Output is correct
129 Correct 115 ms 78712 KB Output is correct
130 Correct 119 ms 79052 KB Output is correct
131 Correct 1 ms 212 KB Output is correct