Image Filter
InterviewTime limit1sMemory limit1024 MB
For each cell of a heightmap, pick the prediction filter and residual with the smallest absolute error, breaking ties by the lexicographically smallest pair.
- Level
Easy3 of 10
- Topics
- Implementation, Greedy
- Solved
- No attempts yet
Problem
Problem setters exchange data and verify each other's work when they prepare a contest. Setter B built all the data for a problem that uses heightmaps and was satisfied with it.
Only the verification step was left, and then B found a problem. The heightmaps were saved as 16-bit grayscale bitmaps without much thought, and the files were too large for setters to send to each other in real time.
To solve this, a bitmap compression method was designed that uses filters. Compression processes each cell in order. Let be the height at row and column of the heightmap. When the cell being compressed is at row and column , define
- , or if that cell does not exist
- , or if that cell does not exist
- , or if that cell does not exist
Each height in the compressed image is expressed with one of the five filters in the table below. A compressed cell is written as the filter number and the difference between the height and the prediction made by that filter.
For example, suppose the following by heightmap is given.
6 4
2 1
Consider how to express the 1 at the bottom right.
- With filter , the difference from the prediction is
- With filter , the difference is
- With filter , the difference is
- With filter , the difference is
- With filter , the difference is
The total of the absolute differences over the whole compressed image must be minimized over all possible ways of choosing filters. If several choices reach the minimum, pick the one whose sequence of (filter number, difference) pairs is lexicographically smallest when the cells are listed from the top left in column order. Write a program that performs this compression.
Input
The input consists of test cases. The first line contains .
The first line of each test case contains the size of the heightmap, and (, ), separated by a space. The heightmap has rows and columns. The next lines each contain integers (), the pixel values of the heightmap, separated by spaces.
Output
For each test case, print lines. Each line contains pairs, separated by spaces, where each pair is the filter number used and the integer difference from that filter's prediction.