Posterized
Contest: Codeforces Round 480 (Div. 2)
Difficulty rating: 1700
Tags: games, greedy

Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.

Their algorithm will be tested on an array of integers, where the $$$i$$$-th integer represents the color of the $$$i$$$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).

To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $$$k$$$, and each color should belong to exactly one group.

Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.

To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $$$k$$$ to the right.

To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.

Input format:
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq k \leq 256$$$), the number of pixels in the image, and the maximum size of a group, respectively.

The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \leq p_i \leq 255$$$), where $$$p_i$$$ is the color of the $$$i$$$-th pixel.

Output format:
Print $$$n$$$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
One possible way to group colors and assign keys for the first sample:

Color $$$2$$$ belongs to the group $$$[0,2]$$$, with group key $$$0$$$.

Color $$$14$$$ belongs to the group $$$[12,14]$$$, with group key $$$12$$$.

Colors $$$3$$$ and $$$4$$$ belong to group $$$[3, 5]$$$, with group key $$$3$$$.

Other groups won't affect the result so they are not listed here.

Example input:
4 3
2 14 3 4

Example output:
0 12 3 3

Example input:
5 2
0 2 1 255 254

Example output:
0 1 1 254 254

Editorial:
First, it's obvious that for each group we should choose the color with minimum value as the group's key. Now since we want to find the lexicographically smallest array, we iterate from the leftmost number in the array and minimize it as much as possible without considering the numbers to its right.
There are many ways to implement this, one of them is the following:
Iterate from left to right, for each number $$$x$$$, if it is already assigned to a group then ignore it. Otherwise, check the colors less than $$$x$$$ in decreasing order until you find a color $$$y$$$ that is assigned.
If we can extend the group of $$$y$$$ to include $$$x$$$ and the size won't be exceeding $$$k$$$, then we do extend it and assign all the colors between $$$y+1$$$ and $$$x$$$ to the key of $$$y$$$'s group. If the size will exceed $$$k$$$ or such $$$y$$$ was not found (set it to $$$-1$$$ in this case), we create a new group with key equals to $$$max(y+1,x-k+1)$$$ and assign all colors in the range to it.
The complexity of this solution is $$$O(n + c)$$$, where $$$c$$$ is the size of the color range.