electicode
АсосӣКурсҳоМанбаъҳоМасъалаҳоОлимпиадаи миллӣМусобиқаҳоҶадвали роҳбарон
...

Bookshelf

Маҳдудияти вақт: 1000msМаҳдудияти ҳофиза: 256MB
Ҳамаи ҳалҳо

Тавсифи масъала

Karimjon works at the National Library. In the section where Karimjon works, there is a large bookshelf of size n×mn \times mn×m, where rows are numbered from 000 to n−1n-1n−1 and columns from 000 to m−1m-1m−1.

The cell (i,j)(i, j)(i,j) of the bookshelf contains a book with number a[i][j]a[i][j]a[i][j]. All book numbers on the shelf are distinct.

Karimjon can perform the following two operations:

  • Row rearrangement: For every row, rearrange the books in that row in any order. The order can be chosen independently for each row.
  • Column rearrangement: For every column, rearrange the books in that column in any order. The order can be chosen independently for each column.

Karimjon wants to sort the bookshelf. In a sorted bookshelf, row 000 contains all books with numbers from 000 to m−1m-1m−1 in increasing order, row 111 contains all books with numbers from mmm to 2m−12m-12m−1 in increasing order, and so on. That is, for all and , the condition must hold.

Let RRR be the minimum number of operations needed to sort the bookshelf. You are also given a number TTT, where 1≤T≤21 \leq T \leq 21≤T≤2.

If T=1T = 1T=1, you only need to find the minimum number of operations RRR.

If T=2T = 2T=2, you need to find RRR and also output the sequence of operations that achieves it.

Input Format

The first line contains three integers nnn, mmm, and TTT.

The next nnn lines each contain mmm integers --- the elements a[i][0],a[i][1],…,a[i][m−1]a[i][0], a[i][1], \ldots, a[i][m-1]a[i][0],a[i][1],…,a[i][m−1].

Constraints:

  • 1≤n,m≤1001 \leq n, m \leq 1001≤n,m≤100
  • 1≤T≤21 \leq T \leq 21≤T≤2
  • 0≤a[i][j]≤n⋅m−10 \leq a[i][j] \leq n \cdot m - 10≤a[

Output Format

If T=1T = 1T=1, print a single integer RRR --- the minimum number of operations.

If T=2T = 2T=2, print the operations in order. For each operation, print a line containing either row or col, indicating whether it is a row rearrangement or a column rearrangement. Then print nnn lines, each containing mmm integers --- the state of the bookshelf after applying this operation. Finally, on the last line, print RRR --- the total number of operations performed.

Each row rearrangement must only permute elements within each row (the multiset of elements in each row must remain the same compared to the state before this operation). Each column rearrangement must only permute elements within each column (the multiset of elements in each column must remain the same compared to the state before this operation).

After all operations, the bookshelf must be sorted, i.e. a[i][j]=i⋅m+ja[i][j] = i \cdot m + ja[i][j]=i⋅m+j for all i,ji, ji,j.

The number of operations must equal RRR, the minimum possible.

Scoring

SubtaskAdditional ConstraintsPointsRequired Subtask
000Tests from the example000-
111n,m≤3n, m \leq 3n,m

Notes

Example 1:

Here T=2T = 2T=2. The minimum number of operations is R=1R = 1R=1. We perform one row rearrangement: in row 000, we rearrange [1,0][1, 0][1,0] into [0,1][0, 1][0,1]; in row , we rearrange into .

Example 2:

Here T=1T = 1T=1, so we only need to output R=2R = 2R=2.

Example 3:

The minimum is R=3R = 3R=3. We perform: row rearrangement, then column rearrangement, then row rearrangement. After all three operations, the bookshelf is sorted.

Мисолҳо

Мисол 1
Вуруд
2 2 2
1 0
3 2
Баромад
row
0 1
2 3
1
Мисол 2
Вуруд
2 3 1
0 4 2
5 1 3
Баромад
2
Мисол 3
Вуруд
5 3 2
10 9 11
14 5 12
1 4 7
3 8 13
2 6 0
Баромад
row
11 9 10
12 14 5
4 7 1
8 3 13
0 2 6
col
0 2 1
4 3 5
8 7 6
11 9 10
12 14 13
row
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
3

© 2026 Electicode. All rights reserved.

0≤i≤n−10 \leq i \leq n-1
0≤i≤n−1
0≤j≤m−10 \leq j \leq m-10≤j≤m−1
a[i][j]=i⋅m+ja[i][j] = i \cdot m + ja[i][j]=i⋅m+j
i
]
[
j
]
≤
n⋅
m−
1
  • All elements of the matrix aaa are distinct.
  • ≤
    3
    151515
    -
    222T=1T = 1T=1151515-
    333R≤2R \leq 2R≤2 is guaranteed101010-
    444n≤3n \leq 3n≤3, m≤15m \leq 15m≤15151515111
    555n,m≤15n, m \leq 15n,m≤15181818000, 111, 444
    666No additional constraints272727000, 111, 222, 333, 444, 555
    111
    [3,2][3, 2][3,2]
    [2,3][2, 3][2,3]