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;
char s[7*maxn + 10];
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() {
scanf(" %s", s);
vector<int> open;
for(int i = 0; i < (int)strlen(s); 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);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf(" %s", s);
| ~~~~~^~~~~~~~~~
# | 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... |