#P5910. [CTSC2007] 矩阵Matrix 【征集 SPJ】

[CTSC2007] 矩阵Matrix 【征集 SPJ】

Problem Description

Given an integer DD and a real matrix AA with nn rows and mm columns, the element in row ii and column jj is aija_{ij}, and 0aijD0 \le a_{ij} \le D (1in1 \le i \le n, 1jm1 \le j \le m). You are asked to provide an n×mn \times m 0101 matrix BB, whose element in row ii and column jj is bijb_{ij} (1in1 \le i \le n, 1jm1 \le j \le m), where bijb_{ij} is either 00 or 11.

For the given matrix AA and your matrix BB, we can compute:

$$p_1=\max \begin{cases}\max\limits_{ 1 \le j \le m} \{ |\sum_{i=1}^n (b_{ij}-\frac{a_{ij}}{D})|\}\\\max\limits_{1 \le i \le n} \{ |\sum_{j=1}^m (b_{ij}-\frac{a_{ij}}{D})|\}\end{cases}$$$$p_2=\max_{1 \le i \le n,1 \le j \le m} \{|b_{i,j}+b_{i-1,j}+b_{i,j-1}+b_{i-1,j-1}-\frac{a_{i,j}+a_{i-1,j}+a_{i,j-1}+a_{i-1,j-1}}{D}|\}$$

For different test cases, we hope that the matrix BB you provide can make p1p_1 or p2p_2 as small as possible.

Input Format

The first line contains an integer cc, which has two possible values: c=1c=1 means our minimization target is p1p_1, and c=2c=2 means we want p2p_2 to be as small as possible.

The second line contains three integers DD, nn, and mm, separated by a single space. The meaning of DD is as described above, and nn and mm are the number of rows and columns of matrix AA, respectively.

Then follow nn lines, each containing mm real numbers describing matrix AA. The real number in row ii and column jj is aija_{ij}, and adjacent numbers are separated by a single space.

Output Format

Output only an n×mn \times m 0101 matrix BB, representing the answer you found that makes pcp_c as small as possible. The number in row ii and column jj is bijb_{ij}. Adjacent integers are separated by a single space.

1
7 3 4
1 6 4 6
7 0 3 3
2 5 1 5

0 1 0 1
1 0 1 0
0 1 0 1

2
7 3 4
1 6 4 6
7 0 3 3
2 5 1 5

0 1 0 1
1 0 1 0
0 1 0 1

Hint

For 40%40\% of the testdata, c=1c=1.

For 60%60\% of the testdata, c=2c=2.

For 100%100\% of the testdata, Constraints: 2n,m7002 \le n ,m \le 700, 1D1091 \le D \le 10^9.

Translated by ChatGPT 5