A9971 | Median Smoothing
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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 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 $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 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.
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 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 $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 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.
输入格式
The first input line of the input contains a single integer $n$ ( $3<=n<=500000$ ) — the length of the initial sequence.
The next line contains $n$ integers $a_{1},a_{2},...,a_{n}$ ( $a_{i}=0$ or $a_{i}=1$ ), giving the initial sequence itself.
The next line contains $n$ integers $a_{1},a_{2},...,a_{n}$ ( $a_{i}=0$ or $a_{i}=1$ ), giving the initial sequence itself.
输出格式
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.
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.
输入输出样例
输入 #1
4 0 0 1 1
输出 #1
0 0 0 1 1
输入 #2
5 0 1 0 1 0
输出 #2
2 0 0 0 0 0
In the second sample the stabilization occurs in two steps: , and the sequence $00000$ is obviously stable.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted