题目链接:http://codeforces.com/problemset/problem/660/C

大意是给一个01数组,至多可以将k个0变为1,问最后数组中最长能有多少个连续的1,并输出。

问题转化一下就是找一个区间,使得区间中0的个数不多于k,且区间长度尽可能地长。尺取法做一下就可以了。

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream; public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
} static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt(), k = in.nextInt();
int[] a = new int[n];
int l = 0;
int cnt = 0;
int ret = 0;
int L = -1, R = -1;
for (int r = 0; r < n; r++) {
a[r] = in.nextInt();
cnt += 1 - a[r];
while (cnt > k) {
cnt -= 1 - a[l++];
}
if (r - l + 1 >= ret) {
ret = r - l + 1;
L = l;
R = r;
}
}
out.println(ret);
for (int i = 0; i < n; i++) {
if (L <= i && i <= R) {
out.print(1);
} else {
out.print(a[i]);
}
out.print(" ");
}
} } static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
} }
}

CF #edu 11 C. Hard Process的更多相关文章

  1. Case of the Zeros and Ones 分类: CF 2015-07-24 11:05 15人阅读 评论(0) 收藏

    A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  3. Educational Codeforces Round 11——C. Hard Process(YY)

    C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  5. MySQL 8.0.11 报错[ERROR] [MY-011087] Different lower_case_table_names settings for server ('1')

    --报错信息: 2018-06-07T19:52:26.943083+08:00 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld ...

  6. ILOG JRules 和 WebSphere Process Server 集成概述

    ILOG JRules 和 WebSphere Process Server 集成概述 简介 业务流程管理(Business Process Management,BPM)和业务规则管理系统(Busi ...

  7. [20190418]exclusive latch spin count.txt

    [20190418]exclusive latch spin count.txt--//昨天测试"process allocation" latch,主要这个latch与其它拴锁s ...

  8. SIGSEGV异常时打印函数调用链

    C语言写的程序跑飞了,怎样打印出函数调用链呢? linux_dev_framework软件包中的trace_exception_test.c就是一个实现演示样例. 该程序有益产生一个内存訪问异常,然后 ...

  9. 挑子学习笔记:BIRCH层次聚类

    转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/6129425.html 本文是“挑子”在学习BIRCH算法过程中的笔记摘录,文中不乏一些个人理解,不当之处望 ...

随机推荐

  1. Java面试14|Session与Cookie

    1.在分布式环境,管理Session通常使用下面三种方式: (1)Session Replication 方式管理 (即session复制) 将一台机器上的Session数据广播复制到集群中其余机器上 ...

  2. CentOS 7安装配置FTP服务器

    CentOS 7下FTP服务器的安装配置. 假设我们有以下要求 路径 权限 备注 /ftp/open 公司所有人员包括来宾均可以访问 只读 /ftp/private 仅允许Alice.Jack.Tom ...

  3. 2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][ ...

  4. C#"曾经的字符串数组"string[] array=new string[]{"**","****"};

    写博客是一件很伟大的事情,尤其是也牛逼的博客,因为它能帮助需要的人,更能使自己对知识有一个更为深刻的理解! 欢迎关注我的博客! 字符串操作(取当前时间) string time=convert.tos ...

  5. 关于Maven的安装及初步使用

    关于Maven的初步使用 1.  下载: 进入http://maven.apache.org/download.cgi下载  Maven 3.3.1 2.  将压缩包解压到自己的硬盘中,最好放在某个盘 ...

  6. ListView的简单使用

    首先在主界面建立一个ListView的布局

  7. 实际比较filter2D和imfilter之间的关系

    实际比较filter2D和imfilter之间的关系 ​                  卷积运算是图像处理和增强中经常遇到的一种算法.由于很多优秀的开源算法都是采用matlab编写的,在我改写为c ...

  8. 【转】深入理解RunLoop

    RunLoop 是 iOS 和 OS X 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何 ...

  9. ad_封装_ads828

    module ad_ctrl( clk,rst_n,ad_clk, ad_data,value_x,value_y,q_sig,wren,r_addr,w_addr ); input clk; inp ...

  10. 零基础入门学习UI设计指南

    第一步:认识设计启蒙必备知识 学习一项技能,尤其是已经有一定沉淀并在各行各业有广泛应用的技能,就一定要对它先有充分的认知.在开始正式学习前,你需要花足够的经历去了解和查阅它的起源.发展.应用.未来. ...