Salt and Pepper (Large)
Time limit1sMemory limit512 MB
For each W by W window in the matrix, output the median of its W^2 pixel values.
- Level
Medium6 of 10
- Topics
- Sliding window, Sorting, Matrix
- Solved
- No attempts yet
Problem
Alice took a photo of the cream cheese spaghetti served for dinner. The salt and pepper scattered over the pasta bothered her, so she asked Bob to erase those specks. Bob pulled a sheet of paper and a pen out of his bag and wrote the problem down like this.
- An matrix representing the photo is given as input. Each element of the matrix is the brightness of one pixel, where is the darkest value and is the brightest value.
- An integer is given as well. Matrix is defined from as follows.
Here is the element in row , column of matrix . In other words, is the median of the values inside the square whose top left corner is row , column of .
- Compute matrix from the given matrix and integer .

Figure: how matrix is built from matrix and .
Bob opened his laptop and started writing the program, but it did not go well, and the spaghetti began to go soggy. Write the program for Bob before the spaghetti is ruined.
The median is the value that sits in the middle once the given values are sorted by size. Among the five values 1, 2, 3, 3, 100 the one in the middle is 3, so the median is 3. is odd, so is odd too and the median is always a single value.
Input
The first line contains the matrix size and (), the maximum brightness (), and the integer (), separated by spaces in that order. is odd.
Each of the next lines contains integers. The -th integer on line is the element in row , column of matrix , and it is between and .
Output
Print matrix over lines. Each line holds integers separated by one space.
Hint
If is , every square holds a single value, so equals .
Suppose is and a square holds 5, 1, 2, 12, 10, 3, 8, 12, 19. Sorted, the values are 1, 2, 3, 5, 8, 10, 12, 12, 19, and the fifth value is in the middle, so the median is 8.