Codeforces 1204D2 Kirk and a Binary String (hard version)

在这里插入图片描述

题意

给出一个 串 S S S,求一个串 T T T, 要求,等长,所有区间的 L I S LIS LIS 相等,0 的个数尽可能多

题解

从后向前,保证后面的解都是合法的情况下
如果当前位置的数字是 0
那么,他一定是后面以他为起点的区间的 L I S LIS LIS 的一部分,这就要求 T T T 的对应位置必须为 0, 否则 L I S LIS LIS 长度必然减少

如果当前位置的数字为 1
考虑,以他为起点的所有区间
对于那些 L I S LIS LIS 包含它的区间,就是说 L I S LIS LIS 的首项为 1 的区间,他变为0,对这些区间的 L I S LIS LIS 没有影响(他们的 L I S LIS LIS 长度为 1 的个数)
对于那些 L I S LIS LIS 不包含他的区间,就是说 L I S LIS LIS 的首项为 0 的区间,他变为0,对这些区间的 L I S LIS LIS 会改变

换句话说,若想将 1 变为 0 ,必须保证后面所有的区间的 L I S LIS LIS 长度必须和 1 的个数相等!!!

所以,从后向前统计 0 和 1 的数量,当 1 的个数大于等于 0 的个数时,才可以修改

代码
#include<bits/stdc++.h>
#define N 100010
#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi 3.141592653589793
#define mod 998244353
#define P 1000000007
#define LL long long
#define pb push_back
#define fi first
#define se second
#define cl clear
#define si size
#define lb lower_bound
#define ub upper_bound
#define bug(x) cerr<<#x<<"      :   "<<x<<endl
#define mem(x) memset(x,0,sizeof x)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;

int main(int argc, char const *argv[])
{
	string s;
	cin>>s;
	int cnt=0;
	for(int i=s.si()-1;i>=0;i--)
		if (s[i]=='0') cnt++;
	else 
		if (cnt) cnt--;
	else
		s[i]='0';
	cout<<s;


	return 0;
}
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值