CF #edu 11 C. Hard Process
题目链接: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的更多相关文章
- 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 ...
- 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(YY)
C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- 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 ...
- ILOG JRules 和 WebSphere Process Server 集成概述
ILOG JRules 和 WebSphere Process Server 集成概述 简介 业务流程管理(Business Process Management,BPM)和业务规则管理系统(Busi ...
- [20190418]exclusive latch spin count.txt
[20190418]exclusive latch spin count.txt--//昨天测试"process allocation" latch,主要这个latch与其它拴锁s ...
- SIGSEGV异常时打印函数调用链
C语言写的程序跑飞了,怎样打印出函数调用链呢? linux_dev_framework软件包中的trace_exception_test.c就是一个实现演示样例. 该程序有益产生一个内存訪问异常,然后 ...
- 挑子学习笔记:BIRCH层次聚类
转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/6129425.html 本文是“挑子”在学习BIRCH算法过程中的笔记摘录,文中不乏一些个人理解,不当之处望 ...
随机推荐
- [HDU2037]贪心入门
今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 69 Solved: ...
- 1295: [SCOI2009]最长距离
1295: [SCOI2009]最长距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 960 Solved: 498[Submit][Status ...
- 字符编码的种类:ASCII、GB2312、GBK、GB18030、Unicode、UTF-8、UTF-16、Base64
ASCII码ASCII:https://zh.wikipedia.org/wiki/ASCIIASCII(American Standard Code for Information Intercha ...
- python 接口自动化测试(二)
代码实现 1.XlsEngine.py # -*- coding:utf-8 -*- __author__ = 'yanghaitao' import xlrd import xlwt class X ...
- 安卓Android的内存管理原理解析
Android采取了一种有别于Linux的进程管理策略,有别于Linux的在进程活动停止后就结束该进程,Android把这些进程都保留在内存中,直到系统需要更多内存为止.这些保留在内存中的进程通常情况 ...
- ubuntu下python flask环境搭建
ubuntu下python flask环境搭建 1. 安装pip sudo apt-get install python-dev pyhton-pip 2. 安装virtualenv sudo apt ...
- Ubuntu14.04下搜狗输入法的安装及配置
在搜狗官网上下载相应的版本32/64 搜狗网址:http://pinyin.sogou.com/linux/?r=pinyin 在文件夹中找到下载的搜狗输入法文件(默认位置是Downloads),双击 ...
- Scut游戏引擎改造兼容Codis。
原生的Scut引擎是采用redis来做数据缓存层,引擎在以异步的方式(时间可配置,默认100ms)实现数据同步.为了提高redis的可扩展性.高可用性,把redis换成codis,因为codis有部分 ...
- 使用moment.js轻松管理日期和时间
大家在前端Javascript开发中会遇到处理日期时间的问题,经常会拿来一大堆处理函数才能完成一个简单的日期时间显示效果.今天我给大家介绍一个轻量级的Javascript日期处理类库:moment.j ...