Increasing Table
Limits: 2 sec., 512 MiB
This statement was translated automatically from Ukrainian.
You are given natural numbers \(n, m\), and \(n\cdot m\) integers \(a_1, a_2, \ldots, a_{nm}\). Is it possible to arrange them in an \(n \times m\) table in such a way that:
The numbers in each row are in increasing order, from left to right;
The numbers in each column are in increasing order, from top to bottom?
If yes, output an example of the appropriately filled table.
Input
The first line of the input data contains a single natural number \(t\) — the number of test cases.
The first line of each test case contains two natural numbers \(n, m\) — the dimensions of the table.
The second line of each test case contains \(n \cdot m\) natural numbers \(a_1, a_2, \ldots, a_{nm}\).
Output
For each test case, if it is impossible to fill the table in the
required way, output NO.
Otherwise, output YES. In the \(i\)-th of the following \(n\) lines, output \(m\) numbers \(b_{i, 1}, b_{i, 2}, \ldots, b_{i, m}\) —
the elements of the \(i\)-th row.
If there are several ways to fill the table this way, output any of them.
Constraints
\(1 \leq t \leq 10000\),
\(1\leq n\cdot m \leq 2\cdot 10^5\),
\(1 \leq a_i \leq 10^9\),
The sum of \(n\cdot m\) over all test cases does not exceed \(2\cdot 10^5\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 3 1 4 4 3 2 1 2 3 1 1 2 2 3 3 3 2 1 2 2 3 3 4 | YES 1 2 3 4 NO YES 1 2 2 3 3 4 |
Notes
Examples for the first and third test cases are given in the output data. It can be shown that for the second test case, it is impossible to arrange the numbers satisfying the problem condition.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|