Codeforces Round 659 (Div. 2)
F
Rearrange
Codeforces
未评级
原题
← E→
翻译中…

Koa the Koala has a matrix $A$ of $n$ rows and $m$ columns. Elements of this matrix are distinct integers from $1$ to $n \cdot m$ (each number from $1$ to $n \cdot m$ appears exactly once in the matrix).

翻译中…

For any matrix $M$ of $n$ rows and $m$ columns let's define the following:

翻译中…
  • The $i$-th row of $M$ is defined as $R_i(M) = [ M_{i1}, M_{i2}, \ldots, M_{im} ]$ for all $i$ ($1 \le i \le n$).
  • The $j$-th column of $M$ is defined as $C_j(M) = [ M_{1j}, M_{2j}, \ldots, M_{nj} ]$ for all $j$ ($1 \le j \le m$).
翻译中…

Koa defines $S(A) = (X, Y)$ as the spectrum of $A$, where $X$ is the set of the maximum values in rows of $A$ and $Y$ is the set of the maximum values in columns of $A$.

翻译中…

More formally:

翻译中…
  • $X = \{ \max(R_1(A)), \max(R_2(A)), \ldots, \max(R_n(A)) \}$
  • $Y = \{ \max(C_1(A)), \max(C_2(A)), \ldots, \max(C_m(A)) \}$
翻译中…

Koa asks you to find some matrix $A'$ of $n$ rows and $m$ columns, such that each number from $1$ to $n \cdot m$ appears exactly once in the matrix, and the following conditions hold:

翻译中…
  • $S(A') = S(A)$
  • $R_i(A')$ is bitonic for all $i$ ($1 \le i \le n$)
  • $C_j(A')$ is bitonic for all $j$ ($1 \le j \le m$)
翻译中…

An array $t$ ($t_1, t_2, \ldots, t_k$) is called bitonic if it first increases and then decreases.

翻译中…

More formally: $t$ is bitonic if there exists some position $p$ ($1 \le p \le k$) such that: $t_1 \lt t_2 \lt \ldots \lt t_p \gt t_{p+1} \gt \ldots \gt t_k$.

翻译中…

Help Koa to find such matrix or to determine that it doesn't exist.

翻译中…

Input

翻译中…

The first line of the input contains two integers $n$ and $m$ ($1 \le n, m \le 250$)  — the number of rows and columns of $A$.

翻译中…

Each of the ollowing $n$ lines contains $m$ integers. The $j$-th integer in the $i$-th line denotes element $A_{ij}$ ($1 \le A_{ij} \le n \cdot m$) of matrix $A$. It is guaranteed that every number from $1$ to $n \cdot m$ appears exactly once among elements of the matrix.

翻译中…

Output

翻译中…

If such matrix doesn't exist, print $-1$ on a single line.

翻译中…

Otherwise, the output must consist of $n$ lines, each one consisting of $m$ space separated integers  — a description of $A'$.

翻译中…

The $j$-th number in the $i$-th line represents the element $A'_{ij}$.

翻译中…

Every integer from $1$ to $n \cdot m$ should appear exactly once in $A'$, every row and column in $A'$ must be bitonic and $S(A) = S(A')$ must hold.

翻译中…

If there are many answers print any.

翻译中…

Note

翻译中…

Let's analyze the first sample:

翻译中…

For matrix $A$ we have:

翻译中…
  • $X = \{ \max(R_1(A)), \max(R_2(A)), \max(R_3(A)) \} = \{ 6, 9, 8 \}$
  • $Y = \{ \max(C_1(A)), \max(C_2(A)), \max(C_3(A)) \} = \{ 4, 8, 9 \}$
  • So $S(A) = (X, Y) = (\{ 6, 9, 8 \}, \{ 4, 8, 9 \})$
翻译中…

For matrix $A'$ we have:

翻译中…
  • Note that each of this arrays are bitonic.
  • $X = \{ \max(R_1(A')), \max(R_2(A')), \max(R_3(A')) \} = \{ 9, 8, 6 \}$
  • $Y = \{ \max(C_1(A')), \max(C_2(A')), \max(C_3(A')) \} = \{ 9, 8, 4 \}$
  • So $S(A') = (X, Y) = (\{ 9, 8, 6 \}, \{ 9, 8, 4 \})$
翻译中…

样例

Input #1
3 3
3 5 6
1 7 9
4 8 2
Output #1
9 5 1
7 8 2
3 6 4
InputCopy OutputCopy
Input #2
2 2
4 1
3 2
Output #2
4 1
3 2
InputCopy OutputCopy
Input #3
3 4
12 10 8 6
3 4 5 7
2 11 9 1
Output #3
12 8 6 1
10 11 9 2
3 4 5 7
InputCopy OutputCopy
题目信息
题号
1384F
时间限制
1 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。