Hard ProcessTime
limit:1000MS
Memory Limit:262144KB
64bit IO Format:%i64d & Amp %i64u Submit Status Practice codeforces 660C
Description
You is given an array a with n Span class= "Apple-converted-space" > 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 number s one, as F(a). You can change no more than K zeroes to ones to maximize F(a).
Input
The first line contains Integers n Span class= "Apple-converted-space" > 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≤ a i ≤ 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< c10> A after the changes.
If There is multiple answers, you can print any one of the them.
Sample Input
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
The main point: let the number of K change, so that the number of consecutive 1 is the largest, we can ask for 0 prefix and, and then through two points to find the location; I've been using violence for a long time ...
Two points:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;Const intINF =0x3f3f3f3f;#defineMem (x, y) memset (x,y,sizeof (x))Const intMAXN =300010;intNUM[MAXN];intA[MAXN];intL, N, K;intjsintx) { for(inti =0; i + x <= n; i++){ if(Num[i + x]-num[i] <=k) {L= i +1; return true; } } return false;}intErfen (intLintR) { intMid, ans; while(L <=R) {Mid= (L + r) >>1; if(JS (mid)) {ans=mid; L= Mid +1; } ElseR= Mid-1; } returnans;}intMain () { while(~SCANF ("%d%d", &n, &k)) { inttemp; memset (num,0,sizeof(num)); for(inti =1; I <= N; i++) {scanf ("%d", A +i); Num[i]= Num[i-1] + (a[i] = =0); } intAns = Erfen (0, N); for(inti = L; I < L + ans; i++) {A[i]=1; } printf ("%d\n", ans); for(inti =1; I <= N; i++){ if(I! =1) printf (" "); printf ("%d", A[i]); }puts (""); } return 0;}
Violence timeout:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;Const intINF =0x3f3f3f3f;#defineMem (x, y) memset (x,y,sizeof (x))Const intMAXN =1000100;intNUM[MAXN];intPOS[MAXN];intP[MAXN];intMain () {intN, K; while(~SCANF ("%d%d", &n, &k)) { intGG =0; for(inti =0; I < n; i++) {scanf ("%d", Num +i); if(Num[i] = =1) GG =1; } if(!GG) {printf ("%d\n", K); for(inti =0; I < K; i++){ if(i) printf (" "); printf ("1"); } for(inti = k; I < n; i++){ if(i) printf (" "); printf ("0"); }puts (""); Continue; } intkg =0, ans =0, temp =0, cnt =0, TP =0; for(inti =0; I < n; i++){ if(kg = =0&& Num[i] = =1) {Temp=0; KG=1; CNT=0; for(intj = i; J < N; J + +){ if(Num[j] = =1) {Temp++; } Else{ if(CNT +1> k) Break; Pos[cnt++] =J; Temp++; } } intj =i; while(CNT < K && J >0) {pos[cnt++] = --J; Temp++; } if(Ans <temp) {ans=temp; TP=CNT; for(intj =0; J < TP; J + +) {P[j]=Pos[j]; } } } if(Num[i] = =0) kg =0; } for(inti =0; i < TP; i++){ //printf ("%d", P[i]);Num[p[i]] =1; }//puts ("");printf"%d\n", ans); for(inti =0; I < n; i++){ if(i) printf (" "); printf ("%d", Num[i]); } puts (""); } return 0;}
Hard Process (two points)