网页游戏网站建设,网络搭建及应用技能大赛 试题与答案,专业酒店建设信息网站,山西国人伟业网站传送门 文章目录题意#xff1a;思路#xff1a;题意#xff1a; 思路#xff1a;
首先一个括号序列合法的条件可以转化成两个(左括号代价为111#xff0c;右括号代价为−1-1−1)#xff1a; (1) 左括号个数等于右括号个数。 (2) 括号的前缀和非负。 所以我们直接用线段…传送门
文章目录题意思路题意 思路
首先一个括号序列合法的条件可以转化成两个(左括号代价为111右括号代价为−1-1−1) (1) 左括号个数等于右括号个数。 (2) 括号的前缀和非负。 所以我们直接用线段树维护一个前缀和序列维护一下最小值即可。要输出的最大嵌套数就是前缀和的最大值再维护一个最大值即可。
//#pragma GCC optimize(2)
#includecstdio
#includeiostream
#includestring
#includecstring
#includemap
#includecmath
#includecctype
#includevector
#includeset
#includequeue
#includealgorithm
#includesstream
#includectime
#includecstdlib
#define X first
#define Y second
#define L (u1)
#define R (u1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].ltr[u].r1)
#define Len(u) (tr[u].r-tr[u].l1)
#define random(a,b) ((a)rand()%((b)-(a)1))
#define db puts(---)
using namespace std;//void rd_cre() { freopen(d://dp//data.txt,w,stdout); srand(time(NULL)); }
//void rd_ac() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//AC.txt,w,stdout); }
//void rd_wa() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//WA.txt,w,stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pairint,int PII;const int N1000010,mod1e97,INF0x3f3f3f3f;
const double eps1e-6;int n;
char s[N],a[N];
struct Node
{int l,r;int sum,lazy;int mx,mi;
}tr[N2];void pushup(int u)
{tr[u].sumtr[L].sumtr[R].sum;tr[u].mxmax(tr[L].mx,tr[R].mx);tr[u].mimin(tr[L].mi,tr[R].mi);
}void pushdown(int u)
{if(tr[u].lazy){int lazytr[u].lazy; tr[u].lazy0;tr[L].lazylazy; tr[R].lazylazy;tr[L].milazy,tr[L].mxlazy;tr[R].milazy,tr[R].mxlazy;}
}void build(int u,int l,int r)
{tr[u]{l,r};tr[u].mi0; tr[u].mx0;if(lr) return;build(L,l,Mid); build(R,Mid1,r);
}void modify(int u,int l,int r,int x)
{if(tr[u].lltr[u].rr){tr[u].lazyx;tr[u].mix,tr[u].mxx;return;}pushdown(u);if(lMid) modify(L,l,r,x);if(rMid) modify(R,l,r,x);pushup(u);
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf(%d%s,n,s1);build(1,1,n1);int pos1,cnt10,cnt20;for(int i1;in;i){if(s[i]R) pos;else if(s[i]L) posmax(1,pos-1);else if(s[i](){if(a[pos]!(){if(a[pos])) modify(1,pos,n1,2),cnt1,cnt2--;else modify(1,pos,n1,1),cnt1;a[pos](;}}else if(s[i])){if(a[pos]!)){if(a[pos]() modify(1,pos,n1,-2),cnt1--,cnt2;else modify(1,pos,n1,-1),cnt2;a[pos]);}}else{if(a[pos])) modify(1,pos,n1,1),cnt2--;else if(a[pos]() modify(1,pos,n1,-1),cnt1--;a[pos]s[i];}if(cnt1!cnt2||tr[1].mi0) printf(-1 );else printf(%d ,tr[1].mx);}return 0;
}
/**/