当前位置: 首页 > news >正文

codeforces 660C C. Hard Process(二分)

题目链接:

C. Hard Process

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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).

 

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.

 

Examples
input
7 1
1 0 0 1 1 0 1
output
4
1 0 0 1 1 1 1
input
10 2
1 0 0 1 0 1 0 1 0 1
output
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+4;
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-1]<=k)return 1;
    return 0;
}
int bis(int num)
{
    int l=1,r=num,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(check(mid,num))r=mid-1;
        else l=mid+1;
    }
    return r+1;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(!a[i])b[i]=b[i-1]+1;
        else b[i]=b[i-1];
    }
    int ans=0,l=0,r=0;
    for(int i=1;i<=n;i++)
    {
        int fx=bis(i);
        if(i-fx+1>ans)
        {
            ans=i-fx+1;
            l=fx;
            r=i;
        }
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++)
    {
        if(i>=l&&i<=r)
        {
            printf("1 ");
        }
        else printf("%d ",a[i]);
    }
    return 0;
}

 



转载于:https://www.cnblogs.com/zhangchengc919/p/5370488.html

相关文章:

  • 广搜最短路(最短时间到达目的地),POJ(3669)
  • 06章 初始继承和多态
  • Paragon NTFS for Mac® Yosemite - 免费下载
  • 全栈工程师的未来发展如何?
  • Linux内核分析8
  • CentOS 7.x设置自定义开机启动,添加自定义系统服务
  • linux中萌翻了的cowsay命令
  • UVA 10129 Play on Words (欧拉通路)
  • 数据库 -- SQL 和 NoSQL 的区别
  • 进度条(第七周)
  • JAVA中十四种常见开发工具及其特点
  • spring Thymeleaf 中文乱码 (转)
  • BZOJ4380: [POI2015]Myjnie
  • iOS开发经验总结
  • 人机交互——对搜狗输入法的评价
  • [PHP内核探索]PHP中的哈希表
  • 收藏网友的 源程序下载网
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 【剑指offer】让抽象问题具体化
  • Computed property XXX was assigned to but it has no setter
  • Go 语言编译器的 //go: 详解
  • js作用域和this的理解
  • Kibana配置logstash,报表一体化
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 将回调地狱按在地上摩擦的Promise
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 网络应用优化——时延与带宽
  • 一个SAP顾问在美国的这些年
  • 追踪解析 FutureTask 源码
  • 树莓派用上kodexplorer也能玩成私有网盘
  • # .NET Framework中使用命名管道进行进程间通信
  • #!/usr/bin/python与#!/usr/bin/env python的区别
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (14)Hive调优——合并小文件
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (第一天)包装对象、作用域、创建对象
  • (论文阅读11/100)Fast R-CNN
  • (南京观海微电子)——COF介绍
  • (三)终结任务
  • (已解决)什么是vue导航守卫
  • (原)Matlab的svmtrain和svmclassify
  • (转)c++ std::pair 与 std::make
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • .cn根服务器被攻击之后
  • .NET Project Open Day(2011.11.13)
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .Net程序帮助文档制作
  • .net对接阿里云CSB服务
  • .NET开源项目介绍及资源推荐:数据持久层
  • .net专家(高海东的专栏)