AtCoder Beginner Contest 428-C Brackets Stack Query题解代码

题目传送门

``

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int q,b=0,mib=0;//平衡度  过程中的最小平衡度
//满足最终平衡度为0 
//过程中的最小平衡度为0
//() 遇见(b+1  遇见)b-1 
//最终只要是好的括号就能保证b=0
//mib必须大于=0  例如)(
//过程中mib会=-1  这是一个坏的序列 
stack<pair<int,int> > st; 
int op;
char c;
int main() {
	scanf("%d",&q);
	while(q--)
	{
		
		scanf("%d",&op);
		if(op==1)
		{
			scanf(" %c", &c);
			st.push({b,mib});
			if(c=='('){
				b++;
			}else{
				b--;
			}
			mib=min(mib,b);
		}
		else
		{
			auto pre=st.top();
			st.pop();
			b=pre.first;
			mib=pre.second;
		}
		if(b==0&&mib>=0)
			printf("Yes\n");
		else
			printf("No\n");
		
	}
    return 0;
}
posted @ 2025-10-24 11:12  mark2025  阅读(6)  评论(0)    收藏  举报