A13315 | Median Mountain Range
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Berland — is a huge country with diverse geography. One of the most famous natural attractions of Berland is the "Median mountain range". This mountain range is $n$ mountain peaks, located on one straight line and numbered in order of $1$ to $n$ . The height of the $i$ -th mountain top is $a_i$ .
"Median mountain range" is famous for the so called alignment of mountain peaks happening to it every day. At the moment of alignment simultaneously for each mountain from $2$ to $n - 1$ its height becomes equal to the median height among it and two neighboring mountains. Formally, if before the alignment the heights were equal $b_i$ , then after the alignment new heights $a_i$ are as follows: $a_1 = b_1$ , $a_n = b_n$ and for all $i$ from $2$ to $n - 1$ $a_i = \texttt{median}(b_{i-1}, b_i, b_{i+1})$ . The median of three integers is the second largest number among them. For example, $\texttt{median}(5,1,2) = 2$ , and $\texttt{median}(4,2,4) = 4$ .
Recently, Berland scientists have proved that whatever are the current heights of the mountains, the alignment process will stabilize sooner or later, i.e. at some point the altitude of the mountains won't changing after the alignment any more. The government of Berland wants to understand how soon it will happen, i.e. to find the value of $c$ — how many alignments will occur, which will change the height of at least one mountain. Also, the government of Berland needs to determine the heights of the mountains after $c$ alignments, that is, find out what heights of the mountains stay forever. Help scientists solve this important problem!
"Median mountain range" is famous for the so called alignment of mountain peaks happening to it every day. At the moment of alignment simultaneously for each mountain from $2$ to $n - 1$ its height becomes equal to the median height among it and two neighboring mountains. Formally, if before the alignment the heights were equal $b_i$ , then after the alignment new heights $a_i$ are as follows: $a_1 = b_1$ , $a_n = b_n$ and for all $i$ from $2$ to $n - 1$ $a_i = \texttt{median}(b_{i-1}, b_i, b_{i+1})$ . The median of three integers is the second largest number among them. For example, $\texttt{median}(5,1,2) = 2$ , and $\texttt{median}(4,2,4) = 4$ .
Recently, Berland scientists have proved that whatever are the current heights of the mountains, the alignment process will stabilize sooner or later, i.e. at some point the altitude of the mountains won't changing after the alignment any more. The government of Berland wants to understand how soon it will happen, i.e. to find the value of $c$ — how many alignments will occur, which will change the height of at least one mountain. Also, the government of Berland needs to determine the heights of the mountains after $c$ alignments, that is, find out what heights of the mountains stay forever. Help scientists solve this important problem!
输入格式
The first line contains integers $n$ ( $1 \le n \le 500\,000$ ) — the number of mountains.
The second line contains integers $a_1, a_2, a_3, \ldots, a_n$ ( $1 \le a_i \le 10^9$ ) — current heights of the mountains.
The second line contains integers $a_1, a_2, a_3, \ldots, a_n$ ( $1 \le a_i \le 10^9$ ) — current heights of the mountains.
输出格式
In the first line print $c$ — the number of alignments, which change the height of at least one mountain.
In the second line print $n$ integers — the final heights of the mountains after $c$ alignments.
In the second line print $n$ integers — the final heights of the mountains after $c$ alignments.
输入输出样例
输入 #1
5 1 2 1 2 1
输出 #1
2 1 1 1 1 1
输入 #2
6 1 3 2 5 4 6
输出 #2
1 1 2 3 4 5 6
输入 #3
6 1 1 2 2 1 1
输出 #3
0 1 1 2 2 1 1
In the first example, the heights of the mountains at index $1$ and $5$ never change. Since the median of $1$ , $2$ , $1$ is $1$ , the second and the fourth mountains will have height 1 after the first alignment, and since the median of $2$ , $1$ , $2$ is $2$ , the third mountain will have height 2 after the first alignment. This way, after one alignment the heights are $1$ , $1$ , $2$ , $1$ , $1$ . After the second alignment the heights change into $1$ , $1$ , $1$ , $1$ , $1$ and never change from now on, so there are only $2$ alignments changing the mountain heights.
In the third examples the alignment doesn't change any mountain height, so the number of alignments changing any height is $0$ .
In the third examples the alignment doesn't change any mountain height, so the number of alignments changing any height is $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted