# Educational Codeforces Round 11 C. Hard Process 二分

2022年11月24日
0 收藏 807 点赞 5,856 浏览 1336 个字

# 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.

7 1

1 0 0 1 1 0 1

4

1 0 0 1 1 1 1

## 代码

``#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]<<" ";}``

python开发_常用的python模块及安装方法

Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接：http://www.codeforces.com/contest/660/problem/CDes…

zengkefu@server1:/usr/src\$ uname -aLinux server1 4.10.0-19-generic #21…

Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式，并且由于涉及到要把拍到的照片显…

Struts的使用