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 ff first
#define ss second
constexpr int maxn = 1e6+10;
string s;
int idx[7*maxn + 10];
vector<pair<pair<int,int>,int>> itv;
struct NodeAns {
int l, r, sz;
};
NodeAns solve(int x) {
if(itv[x].ff.ff == itv[x].ff.ss) return {1, 1, 1};
auto itvL = solve(x+1), itvR = solve(idx[itv[x+1].ss]);
if(s[itv[x].ff.ff-1] == 'x') // max
return {itvL.l + itvR.l, max(itvL.r + itvR.sz, itvL.sz + itvR.r), itvL.sz + itvR.sz};
else // min
return {min(itvL.l, itvR.l), itvL.r + itvR.r - 1, itvL.sz + itvR.sz};
}
int main() {
ios_base::sync_with_stdio(false);
cin >> s;
vector<int> open;
for(int i = 0; i < (int)s.size(); i++)
if(s[i] == '(') {
if(itv.size() && !itv.back().ss) itv.back().ss = i;
open.push_back(i);
}
else if(s[i] == ')') itv.push_back({{open.back(), i}, 0}), open.pop_back();
else if(s[i] == '?') {
if(itv.size() && !itv.back().ss) itv.back().ss = i;
itv.push_back({{i, i}, 0}); // intervals with same beginning and end are question marks
}
sort(itv.begin(), itv.end()); // sorts by the opening bracket
for(int i = 0; i < (int)itv.size(); i++)
idx[itv[i].ff.ff] = i;
auto [l, r, sz] = solve(0);
printf("%d\n", r-l+1);
}
# | 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... |