Median mountain range

시간 제한2초메모리 제한1024 MB

요약
각 내부 산봉우리의 높이를 자신과 양옆 봉우리 높이의 중앙값으로 동시에 바꾸는 과정을 반복한다. 더 이상 바뀌지 않을 때까지 걸린 횟수를 세고, t=1이면 최종 높이도 출력한다.
난이도

어려움10점 중 8점

유형
시뮬레이션, 구현, 배열
정답자
아직 제출이 없습니다

문제

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 nn mountain peaks, located on one straight line and numbered in order of 11 to nn. The height of the ii-th mountain top is a_ia\_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 22 to n−1n - 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_ib\_i, then after the alignment new heights a_ia\_i are as follows: a_1=b_1a\_1 = b\_1, a_n=b_na\_n = b\_n and for all ii from 22 to n−1n - 1 a_i=a\_i = median(b_i−1,b_i,b_i+1)(b\_{i-1}, b\_i, b\_{i+1}). The median of three integers is the second largest number among them. For example, median(5,1,2)=2(5,1,2) = 2, and median(4,2,4)=4(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 cc --- how many alignments will occur, which will change the height of at least one mountain. Help scientists solve this important problem!

Note that in some test groups, in addition to the cc value, you will need to determine the mountain heights after the cc alignments, i.e. the heights of the mountains as they will stay forever.

입력

The first line contains integers nn and tt (1≤n≤500,0001 \le n \le 500\\,000, 0≤t≤10 \le t \le 1) --- the number of mountains and the parameter describing whether it's required to determine the final heights of the mountains.

The second line contains integers a_1,a_2,a_3,…,a_na\_1, a\_2, a\_3, \ldots, a\_n (1≤a_i≤1091 \le a\_i \le 10^9) --- current heights of the mountains.

출력

In the first line print cc --- the number of alignments, which change the height of at least one mountain.

In case t=1t = 1, the second line should contain nn integers --- the final heights of the mountains after cc alignments.

힌트

In the first example, the heights of the mountains at index 11 and 55 never change. Since the median of 11, 22, 11 is 11, the second and the fourth mountains will have height 1 after the first alignment, and since the median of 22, 11, 22 is 22, the third mountain will have height 2 after the first alignment. This way, after one alignment the heights are 11, 11, 22, 11, 11. After the second alignment the heights change into 11, 11, 11, 11, 11 and never change from now on, so there are only 22 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 00. Since t=0t = 0, you shouldn't print the resulting heights of the mountains.

예제3

  1. 예제 1

    입력
    5 1
    1 2 1 2 1
    
    예상 출력
    2
    1 1 1 1 1
    
  2. 예제 2

    입력
    6 1
    1 3 2 5 4 6
    
    예상 출력
    1
    1 2 3 4 5 6
    
  3. 예제 3

    입력
    6 0
    1 1 2 2 1 1
    
    예상 출력
    0