题目链接:
1 second
256 megabytes
standard input
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).
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.
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.
7 1
1 0 0 1 1 0 1
4
1 0 0 1 1 1 1
10 2
1 0 0 1 0 1 0 1 0 1
5
1 0 0 1 1 1 1 1 0 1 题意: 最多把k个0变成1,变完后连续的最长的全是1的串的长度是多少,并且输出最后得串; 思路: 用一个数组记录当前位一共有多少个0,暴力枚举最长串的最后一位,二分查找最长串的第一个1的位置;更新结果并记录好最长串的开始和结束位置,最后再输出就好啦; AC代码:
/*
2014300227 660C - 5 GNU C++11 Accepted 93 ms 4388 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=3e5+;
typedef long long ll;
const double PI=acos(-1.0);
int n,a[N],k,b[N];
int check(int x,int y)
{
if(b[y]-b[x-]<=k)return ;
return ;
}
int bis(int num)
{
int l=,r=num,mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid,num))r=mid-;
else l=mid+;
}
return r+;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(!a[i])b[i]=b[i-]+;
else b[i]=b[i-];
}
int ans=,l=,r=;
for(int i=;i<=n;i++)
{
int fx=bis(i);
if(i-fx+>ans)
{
ans=i-fx+;
l=fx;
r=i;
}
}
printf("%d\n",ans);
for(int i=;i<=n;i++)
{
if(i>=l&&i<=r)
{
printf("1 ");
}
else printf("%d ",a[i]);
}
return ;
}