#K69072. Lexicographically Smallest Grid
Lexicographically Smallest Grid
Lexicographically Smallest Grid
You are given a grid of integers with N rows and M columns, along with an integer K which represents the number of operations allowed. In one operation, you can perform any rearrangement of the grid elements. Your task is to find the lexicographically smallest grid that can be obtained by performing exactly K
operations. In this problem, the optimal strategy turns out to be simply sorting all the elements in non-decreasing order and placing them back into the grid row by row.
Note: Although the integer K
is given as part of the input, it does not affect the procedure since the lexicographically smallest grid is always obtained by sorting the complete list of grid elements.
Example:
Input: 1 3 3 2 9 3 2 1 8 7 5 6 4</p>Output: 1 2 3 4 5 6 7 8 9
inputFormat
The first line of input contains a single integer T
representing the number of test cases. Each test case starts with a line containing three integers N
, M
, and K
where N
is the number of rows, M
is the number of columns and K
is the number of operations allowed. It is followed by N
lines, each containing M
space-separated integers representing the grid.
Input is read from standard input (stdin).
outputFormat
For each test case, print the lexicographically smallest grid, i.e., the grid formed by sorting all the numbers in non-decreasing order and placing them back into an N x M
grid row by row. Each row should be printed on a new line with space-separated integers.
Output should be written to standard output (stdout).
## sample1
3 3 2
9 3 2
1 8 7
5 6 4
1 2 3
4 5 6
7 8 9
</p>