Educational Codeforces Round 11——C. Hard Process(YY)
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
记录0出现的位置,随便写几组数据可以发现要判断起始点是否为0的情况。
若起始点为0,则要判断sum[r]-sum[l]+1<=k;否则要判断sum[r]-sum[l]<=k。然后再判断一下k是否为0即可
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int N=300010;
int sufix[N];
int pos[N];
int main (void)
{
ios::sync_with_stdio(false);
int n,k,i,j;
while (cin>>n>>k)
{
memset(sufix,0,sizeof(sufix));
memset(pos,0,sizeof(pos));
for (i=1; i<=n; i++)
{
cin>>pos[i];
sufix[i]=sufix[i-1]+(pos[i]==0);
}
int l,r,dx,al,ar;
l=1,r=1,dx=0,ar=al=0;
while (l<=n&&r<=n)
{
bool flag=0;
if(pos[l])
{
if(sufix[r]-sufix[l]<=k)
{
if(r-l+1>=dx)
{
dx=r-l+1;
al=l;
ar=r;
}
}
else
{
l++;
}
r++;
}
else
{
if(sufix[r]-sufix[l]+1<=k)
{
if(r-l+1>=dx)
{
dx=r-l+1;
al=l;
ar=r;
}
}
else
{
l++;
}
r++;
}
}
for (i=al; i<=ar; i++)
{
pos[i]=1;
}
if(ar==0&&al==0)
cout<<0<<endl;
else
cout<<ar-al+1<<endl;
for (i=1; i<=n; i++)
{
if(i==n)
cout<<pos[i]<<endl;
else
cout<<pos[i]<<" ";
}
}
return 0;
}
Educational Codeforces Round 11——C. Hard Process(YY)的更多相关文章
- Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- Educational Codeforces Round 11——A. Co-prime Array(map+vector)
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 15 C. Cellular Network(二分)
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 43 E. Well played!(贪心)
E. Well played! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
随机推荐
- 香港城市大学:全球首创3D打印微型机器人技术 有望作治疗癌症用途
香港城市大学(香港城大)的研究团队开发出了全球首创以磁力控制的3D打印微型机器人,该微型机器人技术能做到在生物体内精准运载细胞到指定的位置.新研发的微型机器人有望应用在治疗癌症的靶向治疗,并为细胞层面 ...
- ajax的dataType有哪些类型?
ajax的dataType有哪些类型? 格式为:dataType:"xxx", •"xml": 返回 XML 文档,可用 jQuery 处理 •"ht ...
- java基础—基础语法2
一.语句
- 汉明码(Hamming Code)原理及实现
汉明码实现原理 汉明码(Hamming Code)是广泛用于内存和磁盘纠错的编码.汉明码不仅可以用来检测转移数据时发生的错误,还可以用来修正错误.(要注意的是,汉明码只能发现和修正一位错误,对于两位或 ...
- Python 消息框的处理
在实际系统中,在完成某些操作时会弹出对话框来提示,主要分为"警告消息框","确认消息框","提示消息对话"三种类型的对话框. 1.警告消息框 ...
- pandas中的随机排序和抽样
1.随机重排序 使用take()随机排序 如: df.take([54]) #采取索引为54的数据 可以借助np.random.permutation()函数随机排序 permutation()函 ...
- postman测试传入json
- centos 7 忘记root 密码
@@@@首先开启系统,出现下图界面以后,按e键. @@@使用下放下箭头找到图中的位置,在下图中 修改 ro 为 rw , 添加init=sysroot/bin/sh @@@按Ctrl + x 进入单用 ...
- thinkcmf常用标签
1.图片地址:{:cmf_get_image_url($vo.icon)} 2.模板控件 模板变量调用:$theme_vars.title <widget name="aboutUs& ...
- 小试nginx日志分析xlog
nginx配置: http { #...其他配置 log_format tpynormal '$remote_addr | [$time_local] | $host | "$request ...