CF1861C Queries for the Array 题解
违规用户名968688 · · 题解
::::info[题意]{open}
给你一个字符串和一个初始为空的序列,字符串由 +,-,1,0 构成,其中 + 和 - 表示从这个序列中的末尾 加入 0 和 1 表示判断当前所构成的序列能否满足 非单调不降
:::: ::::info[题解]
我们考虑维护
接着我们就可以分情况去讨论。
首先是 +,这个是简单的,直接将
然后是-,首先要让
接下来考虑如何判断 0 和 1,对于 0,如果当前的数字数量
对于 1,如果
:::: ::::info[代码]
#include<bits/stdc++.h>
#define int long long
using namespace std;
char s[200005];
signed main()
{
int t;
scanf("%lld",&t);
while(t--)
{
scanf("%s",s+1);
int flag = 1;
int cnt = 0,cnt1 = 0,cnt2 = 0;
for(int i=1;i<=strlen(s+1);i++)
{
if(s[i]=='0')
{
if(cnt==cnt2||cnt<=1)
{
flag = 0;
}
if(cnt1==0) cnt1 = cnt;
}
if(s[i]=='1')
{
if(cnt1!=0)
{
flag = 0;
}
cnt2 = cnt;
}
if(s[i]=='+') cnt++;
if(s[i]=='-')
{
cnt--;
if(cnt<cnt1) cnt1 = 0;
if(cnt<cnt2) cnt2 = cnt;
}
}
if(flag==1) puts("YES");
else puts("NO");
}
}