Educational Codeforces Round 11(C)尺取+前缀和

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 thank 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


题意:你有个长度为n,只有0||1的数组,你可以最多填充k个1,问最长连续1的序列有多长?输出这个序列




题解:开始以为是DP,最后想了一下,这题可以使用尺取枚举区间,使用前缀和维护区间和,不断的找那个最长的链,记录下来就好啦




#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;

#define N int(6e5)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;

int sufix[N];
int a[N];
int main()
{
#ifdef CDZSC  
	freopen("i.txt", "r", stdin);
	//freopen("o.txt","w",stdout);  
	int _time_jc = clock();
#endif  

	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		a[0] = 0;
		int aL=0, aR=0, lg = 0;
		memset(sufix, 0, sizeof(sufix));
		for (int i = 1; i <= n; i++)scanf("%d", &a[i]);
		int L = 1, R = 1;
		for (int i = 1; i <= n; i++)
		{
			sufix[i] = sufix[i - 1] + a[i];
		}
		while (L <= n&&R <= n)
		{
			
			while (R - L + 1 - (sufix[R] - sufix[L-1]) <= k&&R<=n)
			{
				if (lg < R - L + 1)
				{
					lg = R - L + 1;
					aL = L;
					aR = R;
				}
				R++;
			}
			L++;
		}
		for (int i = aL; i <= aR; i++)
		{
			a[i] = 1;
		}
		printf("%d\n", lg);
		for (int i = 1; i <= n; i++)
		{
			printf("%d ", a[i]);
		}
		puts("");
	}
	return 0;
}






你可能感兴趣的:(Educational Codeforces Round 11(C)尺取+前缀和)