codeforces 660C C. Hard Process(二分)
题目链接:
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 题意: 最多把k个0变成1,变完后连续的最长的全是1的串的长度是多少,并且输出最后得串; 思路: 用一个数组记录当前位一共有多少个0,暴力枚举最长串的最后一位,二分查找最长串的第一个1的位置;更新结果并记录好最长串的开始和结束位置,最后再输出就好啦; AC代码:
/*
2014300227 660C - 5 GNU C++11 Accepted 93 ms 4388 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=3e5+;
typedef long long ll;
const double PI=acos(-1.0);
int n,a[N],k,b[N];
int check(int x,int y)
{
if(b[y]-b[x-]<=k)return ;
return ;
}
int bis(int num)
{
int l=,r=num,mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid,num))r=mid-;
else l=mid+;
}
return r+;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(!a[i])b[i]=b[i-]+;
else b[i]=b[i-];
}
int ans=,l=,r=;
for(int i=;i<=n;i++)
{
int fx=bis(i);
if(i-fx+>ans)
{
ans=i-fx+;
l=fx;
r=i;
}
}
printf("%d\n",ans);
for(int i=;i<=n;i++)
{
if(i>=l&&i<=r)
{
printf("1 ");
}
else printf("%d ",a[i]);
}
return ;
}
codeforces 660C C. Hard Process(二分)的更多相关文章
- Hard Process(二分)
Hard Process Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- hdu 3433 A Task Process 二分+dp
A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- Codeforces 660C Hard Process【二分 Or 尺取】
题目链接: http://codeforces.com/problemset/problem/660/C 题意: 给定0.1组成的数组,可以改变k个0使其为1,问最终可以得到的连续的1的最大长度. 分 ...
- Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...
- Codeforces 660C Hard Process(尺取法)
题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...
- codeforces 660C Hard Process
维护一个左右区间指针就可以. #include<cstdio> #include<cstring> #include<iostream> #include<q ...
- CodeForces 377B---Preparing for the Contest(二分+贪心)
C - Preparing for the Contest Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
随机推荐
- 设计模式 - 代理模式(proxy pattern) 未使用代理模式 具体解释
代理模式(proxy pattern) 未使用代理模式 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 部分代码參考: http://blog.csdn. ...
- java 中 wait和notify的用法
package com.test; public class OutputThread { public static Object lockObj=new Object(); public stat ...
- spring + jodd 实现文件上传
String tempDir = SystemUtil.getTempDir(); // 获得系统临时文件夹 String prefix = UUID.randomUUID().toString(). ...
- 【WPF学习笔记】[转]周银辉之WPF中的动画 && 晓风影天之wpf动画——new PropertyPath属性链
(一)WPF中的动画 动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式 ...
- implode 函数 把数组拼接成字符串
$array( '0'=>1, '1'=>5, '2'=>5 ); $str=imploade(',',$array); echo str;//输出1,5,3
- Spring MVC获得HttpServletRequest
以下代码是获得Spring MVC中的HttpServletRequest ServletRequestAttributes attr = (ServletRequestAttributes) Req ...
- PHP操作:将数据库中的数据保存到Word、Excel中。
1.首先要把word.excel表放到文件的根目录下 2.定义了一个word类 <?php class word { function start() { ob_start(); ob_star ...
- 初学shell,今天遇到由wget下载到本地的网页源代码的乱码问题,无聊的写了一个转码的脚本
今天用wget想下载文件,结果下载了一堆本地的index.html文件,一查看全是乱码,然后百度了一下,网页的编码格式大概有三种: 1.utf-8 2.gb2312 3.gbk 要在网页源码中的< ...
- 【BZOJ1097】[POI2007]旅游景点atr 最短路+状压DP
[BZOJ1097][POI2007]旅游景点atr Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺 ...
- ios之编码规范具体说明
iOS代码规范: 所有代码规范所有遵循苹果sdk的原则,不清楚的请訪问苹果SDK文档或下载官方Demo查看. 1.project部分: 将项目中每一个功能模块相应的源文件放入同一目录下,使用虚拟目录. ...