码迷,mamicode.com
首页 > 其他好文 > 详细

CF #edu 11 C. Hard Process

时间:2016-06-10 06:08:09      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://codeforces.com/problemset/problem/660/C

 

大意是给一个01数组,至多可以将k个0变为1,问最后数组中最长能有多少个连续的1,并输出。

问题转化一下就是找一个区间,使得区间中0的个数不多于k,且区间长度尽可能地长。尺取法做一下就可以了。

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt();
            int[] a = new int[n];
            int l = 0;
            int cnt = 0;
            int ret = 0;
            int L = -1, R = -1;
            for (int r = 0; r < n; r++) {
                a[r] = in.nextInt();
                cnt += 1 - a[r];
                while (cnt > k) {
                    cnt -= 1 - a[l++];
                }
                if (r - l + 1 >= ret) {
                    ret = r - l + 1;
                    L = l;
                    R = r;
                }
            }
            out.println(ret);
            for (int i = 0; i < n; i++) {
                if (L <= i && i <= R) {
                    out.print(1);
                } else {
                    out.print(a[i]);
                }
                out.print(" ");
            }
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

 

CF #edu 11 C. Hard Process

标签:

原文地址:http://www.cnblogs.com/micrari/p/5573070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!