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. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
随机推荐
- SAP CRM Survey调查问卷的存储模型
数据库表CRM_SVY_DB_SVS,通过如下的函数CRM_SVY_DB_SVS_CREATE插入: 可以通过指定的创建者和创建时间很容易查找到特定的Survey: 调查问卷的答案明细以XML的格式存 ...
- db2疑难解决
http://www-01.ibm.com/support/knowledgecenter/?lang=zh#!/SSEPGG_9.5.0/com.ibm.db2.luw.messages.sql.d ...
- Cenos7—安装
1. 进入安装界面 2. 选择语言 3. 进行分区 4. 设置root密码
- css文件和js文件后面带一个问号----2015-1103
经常看一些网站页面源代码中的css文件和js文件后面带一个问号,后面跟着一连串数字或字符,这是干什么用的? 这个方法我也用过,而且很好用?,它的作用有两个:1.作为版本号,让自己方便记忆.查找:2.作 ...
- An Intuitive Explanation of Convolutional Neural Networks
https://ujjwalkarn.me/2016/08/11/intuitive-explanation-convnets/ An Intuitive Explanation of Convolu ...
- VS/Qt C++和Matlab混合编程
最近两天在搞C++和Matlab混合编程,这个中间过程真是让人心酸啊,最后还是搞定成功!现在把这个过程记录一下. 首先自己的电脑本来就安装着matlab2013b,按着网上的说法首先需要输入!mcc, ...
- FastJsonUtils工具类
fastjson是由alibaba开源的一套json处理器.与其他json处理器(如Gson,Jackson等)和其他的Java对象序列化反序列化方式相比,有比较明显的性能优势. 版权声明:本文为博主 ...
- Android Studio中不能显示svn的上传下载两个图标同时version control为灰,不可点击
最近在接触Android Studio,涉及到svn的配置,因为是先安装的svn,后安装的Android Studio,后边同事告诉我, Android Studio 的SVN安装与其他IDE有很大差 ...
- HDU 5525 Product 数论
题意: 给出一个长度为\(n(1 \leq n \leq 10^5)\)的序列\(A_i\),\(N=\prod\limits_{i=1}^{n}i^{A_i}\).求\(N\)的所有约数的乘积. 分 ...
- CodeForces 379F 树的直径 New Year Tree
题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径. 维护树的直径的两个端点U,V,每次计算一下新加进来的叶子节点到U,V两点的距离,如果有更长的就更新. 因为根据树的直径的求法,若出现新的直 ...