CF2159B.Rectangles

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary grid∗^{\text{∗}} GG of dimensions n×mn \times m.

Let us define a rectangle as a tuple (u,d,l,r)(u,d,l,r) that satisfies the following conditions:

  • 1≤u<d≤n1 \le \boldsymbol{u \lt d} \le n;
  • 1≤l<r≤m1 \le \boldsymbol{l \lt r} \le m;
  • Cells (u,l)(u,l), (u,r)(u,r), (d,l)(d,l), (d,r)(d,r) all contain a 11.

Then, the area of a rectangle (u,d,l,r)(u,d,l,r) is defined as (d−u+1)⋅(r−l+1)(d-u+1) \cdot (r-l+1).

For example, consider the following binary grid given below.

\\begin{matrix} 1 & 0 & 1 & 0 & 1 \\\\ 1 & 0 & 1 & 0 & 0 \\\\ 0 & 0 & 1 & 0 & 1 \\end{matrix}

Here, you may see two rectangles (1,2,1,3)(1,2,1,3) and (1,3,3,5)(1,3,3,5)†^{\text{†}}, each of which has area 66 and 99, respectively.

For each cell (i,j)(i,j), find the minimum area of any rectangle (u,d,l,r)(u,d,l,r) such that u≤i≤du \le i \le d and l≤j≤rl \le j \le r.

∗^{\text{∗}}A binary grid is a grid where each cell contains 00 or 11. The cell on the jj-th column of the ii-th row is denoted as cell (i,j)(i,j).

†^{\text{†}}Note that these are the only rectangles in the grid; for example, (1,1,1,5)(1,1,1,5) is not a rectangle as it does not satisfy u<du \lt d.

给你一个 n×mn \times m 的二进制网格∗^{\text{∗}} GG。

我们定义一个矩形为一个满足以下条件的四元组 (u,d,l,r)(u,d,l,r):

  • 1≤u<d≤n1 \le \boldsymbol{u \lt d} \le n;
  • 1≤l<r≤m1 \le \boldsymbol{l \lt r} \le m;
  • 单元格 (u,l)(u,l)、(u,r)(u,r)、(d,l)(d,l)、(d,r)(d,r) 均包含数字 11。

那么,矩形 (u,d,l,r)(u,d,l,r) 的面积定义为 (d−u+1)⋅(r−l+1)(d-u+1) \cdot (r-l+1)。

例如,考虑下面给出的二进制网格:

101011010000101\begin{matrix} 1 & 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 1 \end{matrix}

此处,你可以看到两个矩形 (1,2,1,3)(1,2,1,3) 和 (1,3,3,5)(1,3,3,5)†^{\text{†}},其面积分别为 66 和 99。

对每个单元格 (i,j)(i,j),求满足 u≤i≤du \le i \le d 且 l≤j≤rl \le j \le r 的任意矩形 (u,d,l,r)(u,d,l,r) 的最小面积。

∗^{\text{∗}}二进制网格是一个每个单元格只含 00 或 11 的网格。第 ii 行第 jj 列的单元格记为 (i,j)(i,j)。

†^{\text{†}}注意,该网格中仅存在这两个矩形;例如,(1,1,1,5)(1,1,1,5) 不是矩形,因为它不满足 u<du \lt d。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n,m2 \le n,m, n⋅m≤250 000n\cdot m \le 250\,000).

Each of the nn following lines contains a string of length mm denoting the ii-th row of GG (Gi,j∈0,1G_{i,j} \in {0,1}).

It is guaranteed that the sum of n⋅mn\cdot m over all test cases does not exceed 250 000250\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n,m2 \le n,m,且 n⋅m≤250 000n\cdot m \le 250\,000)。

接下来的 nn 行中,每行包含一个长度为 mm 的字符串,表示矩阵 GG 的第 ii 行(其中 Gi,j∈{0,1}G_{i,j} \in \{0,1\})。

保证所有测试用例的 n⋅mn\cdot m 之和不超过 250 000250\,000。

输出格式

For each test case, output a grid of nn rows and mm columns. On the jj-th column of the ii-th row, you should output:

  • If there exists a rectangle (u,d,l,r)(u,d,l,r) such that u≤i≤du \le i \le d and l≤j≤rl \le j \le r, output the minimum area of any such rectangle;
  • Otherwise, output 00 instead.

对于每个测试用例,输出一个 nn 行 mm 列的网格。在第 ii 行第 jj 列的位置上,应输出:

  • 若存在矩形 (u,d,l,r)(u,d,l,r) 满足 u≤i≤du \le i \le d 且 l≤j≤rl \le j \le r,则输出所有满足条件的矩形的最小面积;
  • 否则,输出 00。

输入输出样例

  • 输入#1

    3
    3 5
    10101
    10100
    00101
    4 6
    011101
    010001
    100010
    101110
    5 5
    11100
    10110
    11111
    01101
    00111

    输出#1

    6 6 6 9 9
    6 6 6 9 9
    0 0 9 9 9
    0 10 8 8 10 10
    0 10 8 8 10 10
    10 10 8 8 10 0
    10 10 8 8 10 0
    6 6 6 0 0
    6 6 4 4 0
    6 4 4 4 6
    0 4 4 6 6
    0 0 6 6 6

说明/提示

The first test case is explained in the statement.

For the third test case, there are six rectangles that cover at least one cell with minimum area:

  • (1,3,1,2)(1,3,1,2), which has area 66;
  • (1,2,1,3)(1,2,1,3), which has area 66;
  • (3,4,2,3)(3,4,2,3), which has area 44;
  • (2,3,3,4)(2,3,3,4), which has area 44;
  • (3,5,4,5)(3,5,4,5), which has area 66;
  • (4,5,3,5)(4,5,3,5), which has area 66.

第一个测试用例已在题目描述中说明。

对于第三个测试用例,共有六个矩形覆盖至少一个单元格且面积最小:

  • (1,3,1,2)(1,3,1,2),其面积为 66;
  • (1,2,1,3)(1,2,1,3),其面积为 66;
  • (3,4,2,3)(3,4,2,3),其面积为 44;
  • (2,3,3,4)(2,3,3,4),其面积为 44;
  • (3,5,4,5)(3,5,4,5),其面积为 66;
  • (4,5,3,5)(4,5,3,5),其面积为 66。

输入解题思路,AI测评打分。不知道怎么写?

首页