C. Median Smoothing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an 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.
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:
b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1.
The median of a set of three numbers is the number that 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 is equal to 1.
In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.
Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once 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 sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.
Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.
Input
The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — 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 will 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
In the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.
题意:把每一个数换成 由他以及相邻的2个数,组成的中位数,序列的第一个最后一个是不变的
思路:列出3位由0,1组成的序列,会发现,只有101,与010是不稳定的状态,其他的组合都是稳定的,那么我们每次操作只要找到101或者010的序列进行变换即可
101与010 最终的 结果是0在一起,1在一起,。而不会出现010,或者101的这样的情况
#include<bits/stdc++.h>
using namespace std;
const int maxn=500011;
const int inf=1<<27;
#define LL long long
#define P pair<int,int>
#define pb push_back
#define cl(a,b) memset(a,b,sizeof(a));
int a[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
int L=0,R=0;
int ans=0;
for(int i=1;i<n;i++){
while(a[R]!=a[R+1]&&R+1<n)R++;
int l=L+1,r=R-1;
int tmp=0;
while(l<=r){
a[l]=a[L];a[r]=a[R];
l++;r--;
tmp++;
}
L=R=i;
ans=max(ans,tmp);
}
printf("%d\n",ans);
for(int i=0;i<n;i++){
printf("%d%c",a[i],i==n-1?'\n':' ');
}
}
return 0;
}