C. Median Smoothing
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read a encyclopedia article that described the method of median smoothing (or median filter) and its Many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.
Applying the simplest variant of median smoothing to the sequence of Numbers a 1, a 2, ..., a N will result a new Sequence b 1, b 2, ..., b n obtained by the following algorithm:
- b 1 = a 1, b n = a N , that's, the first and the last number of the new sequence match the corresponding n Umbers of the original sequence.
- for i = 2, ..., n -1 value b i is equal to The median of three Values a i -1, a i and a i + 1.
The median of a set of three numbers is the number of this goes on the second place, when these three numbers are written In the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 are equal to 1.
In order to do the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.
have made the procedure once, Vasya looked at the resulting sequence and thought:what if I apply the algorithm to it on Ce again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the Seque NCE can stop changing. We say that the sequence is stable, if it does not a change when the median smoothing are applied to it.
Now Vasya wonders, whether the sequence always eventually becomes stable. He asks to the write a program that, given a sequence of zeros and ones, would determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what would it look like and how many times one needs to APPL Y The median smoothing algorithm to initial sequence on order to obtain a stable one.
Input
The first input line of the input contains a single integer n (3≤ n ≤500)-the length of The initial sequence.
The next line contains n integers a1, a2, ..., an ( ai = 0 or ai = 1), giving the initial sequence itself.
Output
If the sequence would never become stable, print a single number -1.
Otherwise, first print a single integer-the minimum number of times one needs to apply the median smoothing algorithm to The initial sequence before it becomes is stable. In the second line print n numbers separated by a space-the resulting sequence itself.
input
4
0 0 1 1
Output
0
0 0 1 1
Note
The second sample the stabilization occurs in the steps:, and the sequence 00000 are obviously stable.
Test Instructions : give you a 01 string of N, in a transformation a[i]= (a[i-1],a[i],a[i+1]) median, ask you after several transformations, make a stable;
The puzzle: We list can be found only 01010...,1010 ..., only to change, for the length of Len is even transform the number of times is (len-1)/2;
For odd numbers can only be changed to 00000 or 11111 ....
For even numbers can only become 000111 or 111000 ...
So we'll walk through it and we'll find the answer. 0 (n);
///1085422276#include <bits/stdc++.h>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#defineMeminf (a) memset (A,127,sizeof (a));#defineINF 100000007inline ll read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){ if(ch=='-') f=-1; ch=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar (); }returnx*F;}//****************************************#defineMAXN 500000+5intA[maxn],len;intMain () {intn=read (); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); } intans=0; for(intI=2; i<n;i++){ intj=i; while(ABS (a[j]-a[j-1])==1&&j<=N) {J++; }if(J-1) <3)Continue;//cout<<i-1<< "" <<j<<endl; if(J (-1))%2) {len=j-(I-1); Ans=max (ans, (len-1)/2); for(intk=i;k<j;k++) {A[k]=a[i-1]; }i=j-1; }Else{len=j-(I-1); Ans=max (ans, (len-1)/2); for(intk=i;k<=len/2+i-2; k++) {A[k]=a[i-1]; } for(intk=len/2+i-1; k<j;k++) {A[k]=a[j-1]; }}}cout<<ans<<Endl; for(intI=1; i<=n;i++) {cout<<a[i]<<" "; } return 0;}Code
Codeforces Round #327 (Div. 2) C. Median Smoothing Structure