codeforces 660 C. Hard Process (ruler)

cf660C

solution:ruler.1A

/* ***********************************************
Author :111qqz
Created Time :2016年06月08日 星期三 23时43分18秒
File Name :code/cf/problem/660C.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=3E5+7;
7int n,k;
8int sum[N],a[N];
1void ruler()
2{
3    int head = 1;
4    int tail = 1;
5    int l,r;
6    int  res = -1;
7    int cnt = 0 ;
1    while (tail<=n)
2    {
3	while (a[tail]==1) tail++;
4//	cout<<"head:"<<head<<" tail:"<<tail<<endl;
5	if (a[tail]==0&&tail<=n) cnt++;
1	while (sum[tail]-sum[head-1]<=k&&tail<=n) tail++;
2//	cout<<"head:"<<head<<"tail:"<<tail<<endl;
3	    if (tail-head>res)
4	    {
5		res = tail-head;
6	//	cout<<"res:"<<res<<endl;
7		l = head;
8		r = tail-1;
9	    }
1	while (head<=tail&&sum[tail]-sum[head-1]>k) head++;
2//	cout<<"head::"<<head<<" tail:"<<tail<<endl;
3	if (tail<=n&&tail-head+1>res)
4	{
5	    res = tail-head+1;
6	    l = head;
7	    r = tail;
8	}
    }

    
    for ( int i = l ; i <= r ; i++) a[i] = 1;
1    cout<<res<<endl;
2    for ( int i = 1 ; i <= n ; i++) cout<<a[i]<<" ";
3}
4int main()
5{
6	#ifndef  ONLINE_JUDGE 
7	freopen("code/in.txt","r",stdin);
8  #endif
	cin>>n>>k;
1	sum[0] = 0;
2	for ( int i = 1; i <= n ; i++)
3	{
4	    scanf("%d",&a[i]);
5	    sum[i] = sum[i-1] +(1-a[i]);
6	}
	ruler();
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}