Kirk and a Binary String(思维)

题目链接: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 进行分析。
  2. 如果这个位置的 1 可以变成 0,并且不影响他后面所有区间的 LIS 长度,那么他后面所有的 LIS 一定以 1 开头,如果不是的话,这个位置变为 0 之后,LIS 必定会变长。
  3. 那么既然 LIS 是以 1 开头的话,就说明了 LIS 存在的区间中 1 的数量肯定大于等于0的数量。
  4. 所以我们只需要统计当前 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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值