#K72062. Stabilizing Chemical Concentrations
Stabilizing Chemical Concentrations
Stabilizing Chemical Concentrations
You are given an integer ( n ) and a sequence of ( n ) integer concentrations representing measurements along a chemical chain. In one adjustment stage, each concentration (except the first and last) is replaced by the median of its own value and those of its immediate neighbors. The median of three numbers ( a, b, c ) is computed as the middle value after sorting them, i.e. ( \mathrm{median}(a,b,c) = ) the second smallest element of ( {a,b,c} ). The process is repeated until no concentration changes between consecutive stages. Your task is to determine the number of adjustment stages required for stabilization and the final stable concentrations.
Example: If ( n = 5 ) and the initial concentrations are: 3 4 2 4 3, the process will stabilize after 2 stages with final concentrations: 3 3 3 3 3.
inputFormat
The input is read from standard input in the following format:
The first line contains an integer ( n ) (the number of concentrations). The second line contains ( n ) space-separated integers representing the initial concentrations.
outputFormat
Output to standard output in two lines. The first line contains an integer representing the number of adjustment stages required until stabilization. The second line contains ( n ) space-separated integers representing the final stable concentrations.## sample
5
3 4 2 4 3
2
3 3 3 3 3
</p>