CF2248F.Matrix Elimination

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an integer kk and a matrix vv with nn rows and mm columns. The element in row ii and column jj is denoted by vi,jv_{i,j}.

A cell (x,y)(x, y) is called a peak if its value is greater than or equal to the sum of the values in all other cells in row xx and column yy. Formally, (x,y)(x, y) is a peak if

v_x,ygesum_substack1leilenineqxv_i,y+sum_substack1lejlemjneqyv_x,j.v\_{x,y} \\ge \\sum\_{\\substack{1 \\le i \\le n \\\\i \\neq x}} v\_{i,y} + \\sum\_{\\substack{1 \\le j \\le m \\\\j \\neq y}} v\_{x,j}.

You may perform the following operation any number of times (possibly zero):

  • Choose four integers xlx_l, xrx_r, yly_l, and yry_r (1≤xl≤xr≤n1 \le x_l \le x_r \le n, 1≤yl≤yr≤m1 \le y_l \le y_r \le m).
  • Subtract 11 from vi,jv_{i,j} for every xl≤i≤xrx_l \le i \le x_r and yl≤j≤yry_l \le j \le y_r.

Find the minimum number of operations required to obtain a matrix with at least kk peaks.

给你一个整数 kk 和一个具有 nn 行 mm 列的矩阵 vv。第 ii 行第 jj 列的元素记为 vi,jv_{i,j}。

若某个单元格 (x,y)(x, y) 的值大于或等于其所在行 xx 和列 yy 中其余所有单元格的值之和,则称该单元格 (x,y)(x, y) 为峰点。形式化地,(x,y)(x, y) 是一个峰点当且仅当

vx,y≥∑1≤i≤ni≠xvi,y+∑1≤j≤mj≠yvx,j.v_{x,y} \ge \sum_{\substack{1 \le i \le n \\ i \neq x}} v_{i,y} + \sum_{\substack{1 \le j \le m \\ j \neq y}} v_{x,j}.

你可以执行以下操作任意多次(包括零次):

  • 选择四个整数 xlx_l、xrx_r、yly_l 和 yry_r(满足 1≤xl≤xr≤n1 \le x_l \le x_r \le n,1≤yl≤yr≤m1 \le y_l \le y_r \le m);
  • 对所有满足 xl≤i≤xrx_l \le i \le x_r 且 yl≤j≤yry_l \le j \le y_r 的单元格 (i,j)(i, j),将 vi,jv_{i,j} 减 11。

求使矩阵中至少包含 kk 个峰点所需的最少操作次数。

输入格式

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 three integers nn, mm, and kk (1≤n,m≤1051 \le n, m \le 10^5, 1≤k≤n⋅m1 \le k \le n \cdot m, n⋅m≤105n \cdot m \le 10^5) — the number of rows, the number of columns, and the required number of peaks.

The ii-th of the next nn lines contains mm integers vi,1,vi,2,…,vi,mv_{i,1}, v_{i,2}, \ldots, v_{i,m} (−109≤vi,j≤109-10^9 \le v_{i,j} \le 10^9).

It is guaranteed that the sum of n⋅mn \cdot m over all test cases does not exceed 10510^5.

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

每个测试用例的第一行包含三个整数 nn、mm 和 kk(1≤n,m≤1051 \le n, m \le 10^5,1≤k≤n⋅m1 \le k \le n \cdot m,且 n⋅m≤105n \cdot m \le 10^5),分别表示行数、列数以及所需的峰的数量。

接下来的 nn 行中,第 ii 行包含 mm 个整数 vi,1,vi,2,…,vi,mv_{i,1}, v_{i,2}, \ldots, v_{i,m}(−109≤vi,j≤109-10^9 \le v_{i,j} \le 10^9)。

保证所有测试用例中 n⋅mn \cdot m 的总和不超过 10510^5。

输出格式

For each test case, output a single integer — the minimum number of operations required to obtain at least kk peaks.

If there is no solution, print a single integer −1-1.

对于每个测试用例,输出一个整数——得到至少 kk 个峰值所需的最少操作次数。

若无解,则输出整数 −1-1。

输入输出样例

  • 输入#1

    16
    3 3 7
    -1 -30 7
    6 -3 22
    1 -18 16
    3 1 2
    100000000
    300000000
    200000000
    1 3 1
    2 3 4
    1 3 1
    1 2 3
    1 2 2
    5 7
    5 2 8
    531959596 -61416172
    425363565 672913308
    981527099 451180253
    -161687136 898803495
    -388356105 897723313
    1 1 1
    0
    1 1 1
    -5
    1 4 3
    -8 -4 4 -11
    1 9 5
    -12 -4 -13 9 -1 -15 6 -15 -4
    1 3 3
    -12 -15 8
    1 5 5
    10 8 -4 0 -4
    1 3 3
    12 9 -1
    1 3 3
    -14 11 -6
    1 6 6
    -1 0 15 3 1 -14
    1 2 2
    1000000000 -1000000000

    输出#1

    2
    100000000
    1
    0
    2
    734592698
    0
    -1
    0
    0
    11
    6
    12
    11
    7
    2000000000

说明/提示

For the first test case, you can perform the following two operations:

  • choose (xl,xr,yl,yr)=(1,3,2,3)(x_l, x_r, y_l, y_r) = (1, 3, 2, 3);
  • choose (xl,xr,yl,yr)=(2,3,1,3)(x_l, x_r, y_l, y_r) = (2, 3, 1, 3).

After these operations, the matrix will be:

−1\color{green}{-1}

−31-31

6\color{green}{6}

55

−5\color{green}{-5}

20\color{green}{20}

0\color{green}{0}

−20\color{green}{-20}

14\color{green}{14}

The seven peaks are highlighted in green. For example:

  • The cell (1,1)(1, 1) is a peak because −1≥5+0−31+6=−20-1 \ge 5 + 0 - 31 + 6 = -20;
  • The cell (3,1)(3, 1) is a peak because 0≥−20+14−1+5=−20 \ge -20 + 14 - 1 + 5 = -2;
  • The cell (2,1)(2, 1) is not a peak because 5<−5+20−1+0=145 \lt -5 + 20 - 1 + 0 = 14.

It can be shown that fewer than two operations cannot create seven peaks, so the answer is 22.

In the eighth test case, the only cell has value −5-5. A cell in a 1×11 \times 1 matrix is a peak if and only if its value is non-negative. Since every operation only decreases its value, it is impossible to make it a peak.

For the last test case, both cells are peaks exactly when their values are equal. Therefore, the first cell must be decreased from 10910^9 to −109-10^9, which requires 2⋅1092 \cdot 10^9 operations.

对于第一个测试用例,你可以执行以下两次操作:

  • 选择 (xl,xr,yl,yr)=(1,3,2,3)(x_l, x_r, y_l, y_r) = (1, 3, 2, 3);
  • 选择 (xl,xr,yl,yr)=(2,3,1,3)(x_l, x_r, y_l, y_r) = (2, 3, 1, 3)。

执行这些操作后,矩阵将变为:

−1\color{green}{-1}

−31-31

6\color{green}{6}

55

−5\color{green}{-5}

20\color{green}{20}

0\color{green}{0}

−20\color{green}{-20}

14\color{green}{14}

其中七个峰值单元格以绿色高亮显示。例如:

  • 单元格 (1,1)(1, 1) 是一个峰值,因为 −1≥5+0−31+6=−20-1 \ge 5 + 0 - 31 + 6 = -20;
  • 单元格 (3,1)(3, 1) 是一个峰值,因为 0≥−20+14−1+5=−20 \ge -20 + 14 - 1 + 5 = -2;
  • 单元格 (2,1)(2, 1) 不是峰值,因为 5<−5+20−1+0=145 \lt -5 + 20 - 1 + 0 = 14。

可以证明:少于两次操作无法构造出七个峰值,因此答案为 22。

在第八个测试用例中,矩阵仅含一个单元格,其值为 −5-5。在 1×11 \times 1 矩阵中,一个单元格是峰值当且仅当其值非负。由于每次操作只会降低该值,因此不可能使其成为峰值。

在最后一个测试用例中,两个单元格均为峰值当且仅当它们的值相等。因此,第一个单元格必须从 10910^9 减少至 −109-10^9,这需要 2⋅1092 \cdot 10^9 次操作。

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

首页