#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 1e3 + 5, K = 8;
string str;
int n;
set<int> commas_at_depth[K * MAX_N]; // Starts at 1
int l[K * MAX_N], r[K * MAX_N], d[K * MAX_N];
int type[K * MAX_N]; // 1 = min, 2 = max, 3 = leaf
void precomp() {
for (int i = 0; i < str.size(); i++) n += (bool) (str[i] == '?');
int depth = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(') depth++;
else if (str[i] == ')') depth--;
else if (str[i] == ',') commas_at_depth[depth].insert(i);
}
l[1] = 0;
r[1] = str.size() - 1;
d[1] = 1;
for (int i = 1; i <= K * n; i++) {
if (d[i] == 0) continue;
// cout << i << " " << l[i] << " " << r[i] << " " << d[i] << endl;
if (l[i] == r[i]) {
type[i] = 3;
continue;
}
type[i] = (str[l[i] + 1] == 'i') ? 1 : 2;
int j = *commas_at_depth[d[i]].lower_bound(l[i]);
l[2 * i] = l[i] + 4; r[2 * i] = j - 1; d[2 * i] = d[i] + 1;
l[2 * i + 1] = j + 1; r[2 * i + 1] = r[i] - 1; d[2 * i + 1] = d[i] + 1;
}
}
pii find_bounds(int u) {
if (type[u] == 3) return {0, 0};
pii l_bounds = find_bounds(2 * u), r_bounds = find_bounds(2 * u + 1);
int min_bound = (type[u] == 1) ? min(l_bounds.first, r_bounds.first) : l_bounds.first + r_bounds.first + 1;
int max_bound = (type[u] == 1) ? l_bounds.second + r_bounds.second + 1 : min(l_bounds.second, r_bounds.second);
return {min_bound, max_bound};
}
int main() {
// freopen("homework.in", "r", stdin);
cin.sync_with_stdio(false);
cin.tie(0);
cin >> str;
precomp();
pii bounds = find_bounds(1);
int ans = n - bounds.first - bounds.second;
cout << ans << '\n';
}
Compilation message
Main.cpp: In function 'void precomp()':
Main.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i = 0; i < str.size(); i++) n += (bool) (str[i] == '?');
| ~~^~~~~~~~~~~~
Main.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0; i < str.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Runtime error |
1 ms |
1372 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Runtime error |
1 ms |
1372 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
24 ms |
20412 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Runtime error |
1 ms |
1372 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
860 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Runtime error |
1 ms |
1372 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |