题目链接:D2. Kirk and a Binary String (hard version)
题意:给你一个01串,你可以改变某些 1 使得串中 0 的个数尽量多,但是改变后的串的任意一个区间的LIS长度与原串相同。串的长度为 1e5 。
参考博客:Codeforces Round #581 (Div. 2)Kirk and a Binary String (hard version)。%%%
思路:
- 我们对串中的 1 进行分析。
- 如果这个位置的 1 可以变成 0,并且不影响他后面所有区间的 LIS 长度,那么他后面所有的 LIS 一定以 1 开头,如果不是的话,这个位置变为 0 之后,LIS 必定会变长。
- 那么既然 LIS 是以 1 开头的话,就说明了 LIS 存在的区间中 1 的数量肯定大于等于0的数量。
- 所以我们只需要统计当前 1 位置之后 0 与 1 的数量。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 100010;
int main()
{
char s[N];
int num0 = 0, num1 = 0;
cin >> s;
int len = strlen(s);
for(int i = len - 1; i >= 0; i--)
{
if(s[i] == '0') num0++;
else
{
if(num1 >= num0) s[i] = '0';
else num1++;
}
}
cout << s << endl;
return 0;
}