#K50197. Matrix Rearrangement for Minimizing Maximum Elements
Matrix Rearrangement for Minimizing Maximum Elements
Matrix Rearrangement for Minimizing Maximum Elements
You are given T test cases. For each test case, you are provided with a square matrix of size N. Your task is to rearrange the matrix by sorting all of its elements in ascending order and placing them row‐wise into a new matrix.
This transformation minimizes the maximum element in any row compared to any arbitrary rearrangement. Formally, if the sorted list of all elements is \(a_1, a_2, \ldots, a_{N^2}\), then the rearranged matrix \(B\) should satisfy:
\(B_{i,j} = a_{i \times N + j + 1}\) for \(0 \leq i, j < N\).
Read the input from stdin
and output the rearranged matrix to stdout
for each test case.
inputFormat
The first line contains an integer T, the number of test cases. For each test case, the first line contains an integer N, representing the size of the matrix. This is followed by N lines, each containing N space-separated integers, which form the matrix.
outputFormat
For each test case, output the rearranged matrix in N lines. Each line must contain N space-separated integers. The matrices for consecutive test cases should be printed one after the other.## sample
1
3
4 1 7
8 2 3
6 5 9
1 2 3
4 5 6
7 8 9
</p>