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 a1, a2, ..., a n would result a new sequence b1, b2, ..., b nobtained 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.
Sample Test (s) Input:
4
0 0 1 1
Output
0
0 0 1 1
Input
5
0 1 0) 1 0
Output
2
0 0 0) 0 0
Note:
The second sample the stabilization occurs in the steps:, and the sequence 00000 are obviously stable.
Test instructions: A sequence A of length n and only 0 and 1 is given to find the minimum number of steps required for the sequence to become a stable sequence, and each transformation is a[0] and a[n-1] unchanged,
Assuming that the sequence changes after each change into B, then when i=1~n-2, b[i] = (A[i-1],a[i], a[i+1]) The median, continuing such a transformation until the value of the sequence can no longer be changed.
It is observed that when a number is equal to the number adjacent to it, it is not changed, only the sequence of 01010101 or 10101010 is changed, and these sequences are eventually changed to 11110000,00001111;
#include <stdio.h>#include<string.h>#include<queue>#include<math.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intn=1e6+Ten;Const intinf=0x3f3f3f3f;intA[n];intMain () {intN, I, J, X, p, q, ans; while(SCANF ("%d", &n)! =EOF) {ans= -INF; for(i =0; I < n; i++) scanf ("%d", &A[i]); I=1; while(I < n1) {J=i; while((a[j-1] = = A[j] | | a[j+1] = = A[j]) && J < n1) J++;///If the number is the same as the number adjacent to it, it doesn't have to be changed.x= J;///x marks the starting position of adjacent unequal sequences while((a[j-1] = A[j] && a[j+1]! = A[j]) && J < n-1) J++;///J final mark the terminating position of adjacent unequal sequences for(p = x, q = j1; P <= Q; p++, q--) {A[p]= a[p-1]; A[Q]= a[q+1]; } ///find a sequence that changes these numbers to the final resultans= Max (ans, (j-x+1)/2);///since such a sequence may be more than one, we just need to find the longest sequence .I=J; } printf ("%d\n", ans); printf ("%d", a[0]); for(i =1; I < n; i++) printf ("%d", A[i]); printf ("\ n"); } return 0;}
Codeforces 590A Median Smoothing