#B4469. 回形座位 / seat

    ID: 16867 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟数学2025O2优化山西科创活动小学活动

回形座位 / seat

Problem Description

The homeroom teacher is arranging seats for your class. Each classroom can be viewed as an N×NN\times N seat matrix, where each seat is represented by 00 or 11: 00 represents a boy, and 11 represents a girl.

Now, a student recorded the seat information of this classroom according to the following rules:

  1. Scanning method: Starting from the top-left corner (first row, first column) of the matrix, traverse the entire matrix in a spiral (rectangular) order—first go from left to right along the first row, then from top to bottom along the remaining part of the last column, then from right to left along the remaining part of the last row, and finally from bottom to top along the remaining part of the first column; after completing the outermost layer, continue traversing the inner matrix in the same way until all seats have been traversed.
  2. Recording rule: Use a series of numbers to alternately represent the consecutive number of boys and girls, where the first number always represents the number of boys (for example: 2,3,1,32,3,1,3 means that in order, there are 22 boys, then 33 girls, then 11 boy, then 33 girls).

For example:

In a 3×33\times3 seat matrix with side length 33, the recording sequence of boys and girls is: 2,3,1,32,3,1,3. The final seating order and result are:

Now, given this recording sequence, please restore the original N×NN\times N seat matrix.

The data guarantees: the sum of all numbers in the recording sequence equals N×NN\times N, and the sequence strictly alternates to represent consecutive numbers of boys and girls.

Input Format

The first line contains two integers KK and NN, representing the length of the recording sequence, and the number of rows and columns of the matrix.

The second line contains KK positive integers separated by spaces, representing the recording sequence of boys and girls.

Output Format

Output NN lines, each containing NN integers (00 or 11) separated by spaces, representing the restored seat matrix.

3 3
3 3 3
0 0 0
0 0 1
0 1 1

Hint

[Sample 11 Explanation]

  • Traversal order: $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,2)\rightarrow(3,1)\rightarrow(2,1)\rightarrow(2,2)$

  • Recording sequence analysis:

    1. First number 33: 33 consecutive boys (corresponding to the first 33 positions: (1,1)(1,1), (1,2)(1,2), (1,3)(1,3));
    2. Second number 33: 33 consecutive girls (corresponding to the next 33 positions: (2,3)(2,3), (3,3)(3,3), (3,2)(3,2));
    3. Third number 33: 33 consecutive boys (corresponding to the last 33 positions: (3,1)(3,1), (2,1)(2,1), (2,2)(2,2)).

[Constraints]

For 10%10\% of the data, N=1N=1;

For another 10%10\% of the data, N=2N=2;

For another 10%10\% of the data, the recording sequence contains only boys.

For another 10%10\% of the data, K=2K=2.

For another 20%20\% of the data, K=N×NK=N\times N and all numbers in the recording sequence are 11.

For 100%100\% of the data, 1N1001\le N\le 100, 1KN×N1\le K\le N\times N, and the sum of the recording sequence equals N×NN\times N.


Translated by DeepSeek-V3