#K79392. Arrange Items in a Box
Arrange Items in a Box
Arrange Items in a Box
You are given an integer number of rows (r) and columns (c) for a rectangular box, along with a list of (r \times c) integer weights. Your task is to arrange these weights into the box such that the difference between the maximum and minimum weight in any row or column is minimized. One optimal approach is to sort the weights in increasing order and fill the box in row-major order. This way, every row and column will have weights as evenly distributed as possible.
Formally, if each row (i) has weights (a_{i1}, a_{i2}, \dots, a_{ic}), you are to minimize (\Delta = \max(a_{ij}) - \min(a_{ij})) for each row and similarly for each column.
inputFormat
The input is given via standard input (stdin) in the following format:
The first line contains two integers (r) and (c) separated by a space, representing the number of rows and columns respectively. The second line contains (r \times c) integers (separated by spaces) representing the weights of the items.
outputFormat
Output to standard output (stdout) the arranged box in (r) lines. Each line should contain (c) integers separated by spaces, representing the weights arranged in that row.## sample
2 3
1 6 4 2 5 3
1 2 3
4 5 6
</p>