给出一个长度为\(n(n\le 10^5)\)的只包含01的字符串。把尽可能多的1变为0,使得对于所有的\(l \in [1,n],r\in [l,n]\),区间\([l,r]\)的最长不下降子序列的长度不变。
【译自官方题解】
可以发现有些字符是确定的(即无法修改)。这些确定的字符满足以下几个条件。
【下方内容为自己YY】
然后考虑如何具体实现。发现上面的5条性质(其实是前3条)与括号序列相同。所以实现就非常简单了。将1看做左括号,0看做右括号。最后将不能匹配的左括号全部变为0即可。
/*
* @Author: wxyww
* @Date: 2019-08-21 15:00:10
* @Last Modified time: 2019-08-21 15:01:55
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
#include<cmath>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N = 100000 + 100;
ll read() {
ll x = 0,f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0',c = getchar();}
return x * f;
}
char s[N];
int a[N],top;
int main() {
scanf("%s",s + 1);
int n = strlen(s + 1);
for(int i = 1;i <= n;++i) {
if(s[i] == '1') a[++top] = i;
else if(top) --top;
}
while(top) s[a[top--]] = '0';
printf("%s",s + 1);
return 0;
}
CF1204D Kirk and a Binary String
原文:https://www.cnblogs.com/wxyww/p/CF1204D.html