Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process
题目连接:
http://www.codeforces.com/contest/660/problem/C
Description
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).
Input
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.
Output
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.
Sample Input
7 1
1 0 0 1 1 0 1
Sample Output
4
1 0 0 1 1 1 1
Hint
题意
你有n个非0就是1的数字,你可以修改最多k个,使得0变成1
然后问你修改之后,最长的连续1的串是多长?
题解:
维护一个前缀0的个数
然后对于每个位置,直接暴力二分就好了,二分这个位置最远能够延展到哪儿
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int n,k;
int a[maxn],sum[maxn];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=1-a[i];
}
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i];
int ans1=0,ans2=0;
for(int i=1;i<=n;i++)
{
int l = i,r = n,ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(sum[mid]-sum[i-1]>k)r=mid-1;
else l=mid+1,ans=mid-i+1;
}
if(ans>ans1)
{
ans1=ans;
ans2=i;
}
}
cout<<ans1<<endl;
for(int i=ans2;i<=n;i++)
{
if(ans1==0)break;
if(a[i]==1)a[i]=0;
if(a[i]==0)ans1--;
}
for(int i=1;i<=n;i++)
cout<<1-a[i]<<" ";
}
Educational Codeforces Round 11 C. Hard Process 二分的更多相关文章
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- 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
A. Co-prime Array http://codeforces.com/contest/660/problem/A 题意:给出一段序列,插进一些数,使新的数列两两成互质数,求插最少的个数,并输 ...
- Educational Codeforces Round 21 D.Array Division(二分)
D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划
E. Different Subsets For All Tuples 题目连接: http://www.codeforces.com/contest/660/problem/E Descriptio ...
- Educational Codeforces Round 11 D. Number of Parallelograms 暴力
D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...
- Educational Codeforces Round 11 B. Seating On Bus 水题
B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...
- Educational Codeforces Round 11 A. Co-prime Array 水题
A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...
随机推荐
- git push multiple repo
git remote add xxx https://git.github.com
- X86控制寄存器和系统地址寄存器
80386控制寄存器和系统地址寄存器如下表所示.它们用于控制工作方式,控制分段管理机制及分页管理机制的实施. 控制寄存器 CRx BIT31 BIT30—BIT12 BIT11—BIT5 BIT4 B ...
- tomcat+java的web程序持续占cpu高问题调试【转】
转自 tomcat+java的web程序持续占cpu问题调试 - 像风一样的自由 - CSDN博客http://blog.csdn.net/five3/article/details/28416771 ...
- HDU 6197 array array array 2017沈阳网络赛 LIS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197 题意:给你n个数,问让你从中删掉k个数后(k<=n),是否能使剩下的序列为非递减或者非递增 ...
- python 之ConfigParser模块学习
1.1 读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该secti ...
- 表格中上移下移置顶的js操作
<script> $(function(){ //上移 var $up = $(".up") $up.click(function() { var $tr = ...
- C# winform或控制台Properties.Settings.Default的使用及存储位置
C# winform或控制台Properties.Settings.Default的使用及存储位置 作者的程序 是MmPS.ClientForm.exe,使用Properties.Settings.D ...
- bind1st bind2nd的使用
STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...
- CRM 业务
1. 创建CRM项目 引入插件 创建数据库 from django.db import models from django.db import models class Department(mod ...
- Delphi XE增强的RTTI妙用--动态创建包中的窗口类
以前要在运行时创建package中的form类,必须要在form单元文件中这样注册类: Initialization RegisterClass(TForm3);Finalization UnRe ...