zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 11C. Hard Process two pointer

    地址:http://codeforces.com/contest/660/problem/C

    题目:

    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

     思路:一开始我用的是n^2的算法,一直tle,后来才知道有种算法叫尺取法:就是动态维护一个长度为x的区间,并同时记录起始位置和终点位置。

      对这题而言,就是维护含0数为k的0,1区间,记录长度最大值,和起始位置和终点位置;

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <queue>
     7 #include <stack>
     8 #include <map>
     9 #include <vector>
    10 
    11 #define PI acos((double)-1)
    12 #define E exp(double(1))
    13 using namespace std;
    14 int a[1000000];
    15 int main (void)
    16 {
    17     int n,k,s=0,e=0,sum=0,len=0;
    18     cin>>n>>k;
    19     for(int i = 1; i<=n; i++)
    20         {
    21             scanf("%d",&a[i]);
    22             sum += (a[i] == 0);
    23             while(sum > k)
    24                 {
    25                     sum -= (a[++s] == 0);
    26                 }
    27             if(len < i - s)
    28                 {
    29                     e = i;
    30                     len = i - s;
    31                 }
    32         }
    33     cout<<len<<endl;
    34     for(int i = 1; i<=n; i++)
    35         if(e>= i && i> e - len )
    36             {
    37                     printf("1 ");
    38             }
    39         else
    40             {
    41                     printf("%d ",a[i]);
    42             }
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    新版Unity的一些小玩意儿
    Three.js踩坑笔记
    Unity 接入腾讯云COS对象存储
    通过贝塞尔曲线编辑曲线
    利用ZXing.Net生成和识别二维码
    开源免费的安卓投屏工具-Scrcpy
    Unity Reflection Probe使用入门
    Unity VFX踩坑
    windows上通过自定义后缀文件启动Unity应用
    应用于Oculus Quest的VR头显应用
  • 原文地址:https://www.cnblogs.com/weeping/p/5371900.html
Copyright © 2011-2022 走看看