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. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
随机推荐
- 并发教程--JAVA5中 计数信号量(Counting Semaphore)例子
并发教程--JAVA5中 计数信号量(COUNTING SEMAPHORE)例子 本文由 TonySpark 翻译自 Javarevisited.转载请参见文章末尾的要求. Java中的计数信息量(C ...
- cv2.getPerspectiveTransform 透视变换
简介 透视变换(Perspective Transformation)是将成像投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping).如图1,通过透 ...
- 《实战Python网络爬虫》- 感想
端午节假期过了,之前一直在做出行准备,后面旅游完又休息了一下,最近才恢复状态. 端午假期最后一天收到一个快递,回去打开,发现是微信抽奖中的一本书,黄永祥的<实战Python网络爬虫>. 去 ...
- 获取地址栏参数,json遍历
1. 获取地址栏参数 GetQueryString: function(name){ // 获取地址栏参数 var reg = new RegExp("(^|&)"+ na ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...
- 【0624作业】使用Scanner类输入并显示会员卡号
package com.work0624; /** * 练习题 * 使用Scanner类输入并显示会员卡号 * @author L */ import java.util.Scanner; publi ...
- String中关于BeanFactory
org.springframework.beans及org.springframework.context包是Spring IoC容器的基础.BeanFactory提供的高级配置机制,使得管理任何性质 ...
- iOS快速开发框架--Bee Framework
Bee Framework是一款iOS快速开发框架,允许开发者使用Objective-C和XML/CSS来进行iPhone和iPad开发,由 Gavin Kwoe 和 QFish 开发并维护. 其早期 ...
- Java基础面试操作题:线程同步代码块 两个客户往一个银行存钱,每人存三十次一次存一百。 模拟银行存钱功能,时时银行现金数。
package com.swift; public class Bank_Customer_Test { public static void main(String[] args) { /* * 两 ...
- skynet 学习笔记-sproto模块(2)
云风在skynet中继承了sproto的传输协议,对比protobuf的好处是,能明文看到传输内容,而且skynet不需要protobuf这么功能,所以云风也建议在lua层使用sproto来作为sky ...