codeforces 660C Hard Process

又不会,又去看了官方的题解。 题目的意思是,给出一个由0和1构成的数组,然后替换其中的k个数字,使得有一个连续由1构成的字串长度最长。用了两个数,表示当前最优的范围,声明了一个变量cnt,用来记录在前一个字串中0的个数,每一次循环结束后,要判断这一次的开头的那个数是否是0,不是0就减1,因为是利用cnt这个在for循环之外的变量来维护的区间中的数,所以要减去旧的区间中开头的那个数。


#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <sstream>

using namespace std;
const int MAX = 3 * 1e5 + 5;
int arr[MAX];

inline void file()
{
    freopen("D:\\go.txt", "r", stdin);
    freopen("D:\\leave.txt", "w", stdout);
}

int main()
{
    //file();
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 0; i != n; ++i)
        scanf("%d", &arr[i]);
    int j = 0, cnt = 0;
    int r = 0, l = 0;
    for (int i = 0; i < n; ++i)
    {
        if (j < i)
        {
            j = i;
            cnt = 0;
        }
        int t_cnt = 0;
        for (; j < n; j++)
        {
           // cout << " j = " << j << endl;
            if (arr[j] == 0)
                t_cnt = cnt + 1;
            if (t_cnt > k)
                break;
            cnt += !arr[j];
        }
        if (j - i > r - l)
        {
           // cout << i << " " << j << endl;
            r = j, l = i;
        }
        if (cnt > 0)
            cnt -= !arr[i];
    }
    printf("%d\n", r - l);
    for (int i = l; i < r; ++i)
        arr[i] = 1;
    for (int i = 0; i < n; ++i)
    {
        if (i)
            putchar(' ');
        printf("%d", arr[i]);
    }
    puts("");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值