P29761 [洛谷] "Stoi2033" Split
普及+
题目描述

Problem Description

There is a $n \times m \times 2$ chess box (with walls on all four sides) and $nm$ black and $nm$ red chess pieces each. The chess pieces come in several types, and the number of red and black chess pieces of the same type is equal. The type of a chess piece is marked by a feature value $v_{i,j}$. Chess pieces with the same feature value are of the same type, and those with different feature values are of different types. The red chess pieces are placed in the lower layer of the chess box, already arranged. Now, Vinsta needs to place the black chess pieces in the upper layer according to a specified order. The coordinates inside the chess box are set with the top-left corner as $(1,1)$ and the bottom-right corner as $(n,m)$, thus the $i$-th row and $j$-th column is $(i,j)$. Each black chess piece placed must be in a position that satisfies the following requirements:

  1. The position is empty and the red chess piece directly below it is of the same type.

  2. Under the condition of 1, if there are multiple positions, the tightness of a position is defined as the number of adjacent black chess pieces or chess box walls on its four sides. Choose the position with the highest tightness.

  3. Under the condition of 2, if there are still multiple positions, choose the one with the smallest sum of coordinates $i+j$.

  4. Under the condition of 3, if there are still multiple positions, choose the one with the smallest $i$.

Given the arrangement of the red chess pieces and the sequence of black chess pieces to be placed, Vinsta would like you to help determine the sequence in which each black chess piece is placed.

Input Format

The first line contains two integers $n, m$.

The next $n$ lines and $m$ columns, where the $i$-th line and $j$-th column represent $v_{i,j}$.

The next line contains $nm$ integers, representing the types of chess pieces to be placed in order.

Output Format

Output $n$ lines and $m$ columns, where each position $(i,j)$ contains the number $k_{i,j}$, indicating that the chess piece at $(i,j)$ is the $k_{i,j}$-th piece to be placed.

Sample Input and Output

Input Sample #1

3 3
1 1 1
1 1 1
1 1 1
1 1 1 1 1 1 1 1 1

Output Sample #1

1 2 3
4 6 7
5 8 9

Input Sample #2

3 3
1 2 3
2 2 1
3 1 3
1 3 3 2 1 2 2 3 1

Output Sample #2

1 4 2
6 7 5
3 9 8

Input Sample #3

10 10
4 9 3 9 3 6 4 8 7 7 
7 5 3 8 7 10 10 8 7 10 
10 9 3 10 3 3 3 2 3 8 
9 6 3 1 10 10 3 4 2 6 
10 5 9 9 5 7 7 6 2 7 
1 1 6 3 2 10 10 7 6 7 
1 7 10 7 3 10 3 9 10 9 
1 5 1 2 2 4 4 9 10 8 
6 3 7 1 5 8 10 4 10 7 
5 4 8 3 3 9 2 6 8 2 
6 6 6 1 10 8 5 5 4 2 1 5 5 9 4 3 4 6 3 5 9 7 4 8 9 3 5 9 1 7 4 1 1 2 2 6 7 10 6 2 6 6 1 8 4 7 7 10 3 1 9 8 10 9 4 7 9 10 2 3 3 3 2 7 2 9 7 7 3 8 8 9 3 2 10 9 10 7 8 10 8 3 7 7 3 3 7 3 7 3 3 10 3 10 10 10 10 10 10 10 

Output Sample #3

9 14 16 28 49 1 15 24 37 46
22 12 26 79 84 94 92 70 56 53
5 21 61 80 85 93 91 63 62 6
25 18 60 50 58 95 90 23 34 3
38 13 51 54 20 87 89 39 59 64
29 32 36 69 74 97 96 78 42 67
11 30 48 68 86 98 88 76 77 72
4 8 33 40 35 31 45 57 99 81
2 19 47 43 27 71 75 55 100 83
7 17 52 73 82 66 65 41 44 10

Notes/Hints

For $30\%$ of the data, $1 \le n, m \le 70$;

For another $30\%$ of the data, $v_{i,j} = 1$;

For $100\%$ of the data, $1 \le n, m \le 10^3, 1 \le v_{i,j} \le 10$, and it is guaranteed that the number of black and red chess pieces of each type is equal.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
样例输出
1 2 3 4 6 7 5 8 9