# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1099655 | Zero_OP | Homework (CEOI22_homework) | C++14 | 220 ms | 214472 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true; return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true; return false;
}
using ll = long long;
using ld = long double;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }
const int MAX = 4e6 + 5;
int len, n;
string s;
int sz[MAX], l[MAX], r[MAX];
bitset<MAX> is_leaf, is_min;
vector<int> adj[MAX];
void dfs(int u){
if(is_leaf[u]){
l[u] = r[u] = 1;
sz[u] = 1;
return;
}
int v1 = adj[u][0];
int v2 = adj[u][1];
dfs(v1); dfs(v2);
sz[u] = sz[v1] + sz[v2];
if(is_min[u]){
l[u] = min(l[v1], l[v2]);
r[u] = r[v1] + r[v2] - 1;
} else{
l[u] = l[v1] + l[v2];
r[u] = max(sz[v1] + r[v2], sz[v2] + r[v1]);
}
}
void testcase(){
cin >> s;
len = sz(s);
stack<int> st;
rep(i, 0, len){
if(s[i] == '('){
++n;
if(!st.empty()){
adj[st.top()].push_back(n);
}
st.push(n);
is_min[n] = (s[i - 1] == 'n');
} else if(s[i] == ')'){
st.pop();
} else if(s[i] == '?'){
++n;
is_leaf[n] = true;
adj[st.top()].push_back(n);
}
}
dfs(1);
cout << r[1] - l[1] + 1 << '\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
#define filename "task"
if(fopen(filename".inp", "r")){
freopen(filename".inp", "r", stdin);
freopen(filename".out", "w", stdout);
}
int T = 1; //cin >> T;
while(T--) testcase();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |