codeforces 660C (尺取法 水~)

C. Hard Process
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a with n elements. Each element of a is either 0 or 1.

Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).

Input

The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k.

The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.

Output

On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones.

On the second line print n integers aj — the elements of the array a after the changes.

If there are multiple answers, you can print any one of them.

Examples
input
7 1
1 0 0 1 1 0 1
output
4
1 0 0 1 1 1 1
input
10 2
1 0 0 1 0 1 0 1 0 1
output
5
1 0 0 1 1 1 1 1 0 1

题意:把最多k个0变成1求最长的连续1的串.

两个指针每次保持之间的0个数恰好是k个.

#include <bits/stdc++.h>
using namespace std;
#define maxn 311111

int a[maxn];
int next[maxn], sum[maxn];
int n, m;

int main () {
   // freopen ("in.txt", "r", stdin);
    cin >> n >> m;
    sum[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum[i] = sum[i-1]+(a[i]==0);
    }
    if (m == 0) {
        int ans = 0, cur = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] == 1)
                cur++;
            else {
                ans = max (ans, cur);
                cur = 0;
            }
        }
        ans = max (ans, cur);
        cout << ans << "\n";
        for (int i = 1; i <= n; i++) cout << a[i] << (i == n ? "\n" : " ");
        return 0;
    }
    int l = 1, r = 1, ans = 0, x, y;
    for (; r < n && sum[r+1] <= m; r++) {
    }
    ans = r-l+1, x = l, y = r;
    for (l = 2; l <= n; l++) {
        while (sum[r+1]-sum[l-1] <= m && r+1 <= n)
            r++;
        if (ans < r-l+1) {
            ans = r-l+1;
            x = l, y = r;
        }
    }
    cout << ans << "\n";
    for (int i = x; i <= y; i++) a[i] = 1;
    for (int i = 1; i <= n; i++) cout << a[i] << (i == n ? "\n" : " ");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值