Codeforces Round 1113 (Div. 2)
F
矩阵消除
Matrix Elimination
Codeforces
2500
原题
← EG →

矩阵消除AI (CI翻译)

You are given an integer $k$ and a matrix $v$ with $n$ rows and $m$ columns. The element in row $i$ and column $j$ is denoted by $v_{i,j}$.

你将得到一个整数 $k$ 和一个有 $n$ 行、$m$ 列的矩阵 $v$。第 $i$ 行第 $j$ 列的元素记为 $v_{i,j}$。AI (CI翻译)

A cell $(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 $x$ and column $y$. Formally, $(x, y)$ is a peak if

如果一个单元格 $(x, y)$ 的值大于或等于第 $x$ 行和第 $y$ 列中所有其他单元格的值之和,则称 $(x, y)$ 为一个峰值。形式化地,$(x, y)$ 是峰值,当且仅当AI (CI翻译)

$$ 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}. $$

$$ 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}. $$AI (CI翻译)

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

你可以执行任意次(也可以一次都不执行)以下操作:AI (CI翻译)

  • Choose four integers $x_l$, $x_r$, $y_l$, and $y_r$ ($1 \le x_l \le x_r \le n$, $1 \le y_l \le y_r \le m$).
  • Subtract $1$ from $v_{i,j}$ for every $x_l \le i \le x_r$ and $y_l \le j \le y_r$.
  • 选择四个整数 $x_l$、$x_r$、$y_l$ 和 $y_r$($1 \le x_l \le x_r \le n$,$1 \le y_l \le y_r \le m$)。
  • 对所有满足 $x_l \le i \le x_r$ 且 $y_l \le j \le y_r$ 的 $v_{i,j}$ 减去 $1$。
AI (CI翻译)

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

求使矩阵中至少有 $k$ 个峰值所需的最少操作次数。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains three integers $n$, $m$, and $k$ ($1 \le n, m \le 10^5$, $1 \le k \le n \cdot m$, $n \cdot m \le 10^5$) — the number of rows, the number of columns, and the required number of peaks.

每个测试用例的第一行包含三个整数 $n$、$m$ 和 $k$($1 \le n, m \le 10^5$,$1 \le k \le n \cdot m$,$n \cdot m \le 10^5$),分别表示行数、列数以及所需的峰值数量。AI (CI翻译)

The $i$-th of the next $n$ lines contains $m$ integers $v_{i,1}, v_{i,2}, \ldots, v_{i,m}$ ($-10^9 \le v_{i,j} \le 10^9$).

接下来的 $n$ 行中,第 $i$ 行包含 $m$ 个整数 $v_{i,1}, v_{i,2}, \ldots, v_{i,m}$($-10^9 \le v_{i,j} \le 10^9$)。AI (CI翻译)

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

保证所有测试用例的 $n \cdot m$ 之和不超过 $10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

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

对于每个测试用例,输出一个整数,即使矩阵中至少有 $k$ 个峰值所需的最少操作次数。AI (CI翻译)

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

如果无解,输出一个整数 $-1$。AI (CI翻译)

Note

说明

AI (CI翻译)

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

对于第一个测试用例,可以执行以下两次操作:AI (CI翻译)

  • choose $(x_l, x_r, y_l, y_r) = (1, 3, 2, 3)$;
  • choose $(x_l, x_r, y_l, y_r) = (2, 3, 1, 3)$.
  • 选择 $(x_l, x_r, y_l, y_r) = (1, 3, 2, 3)$;
  • 选择 $(x_l, x_r, y_l, y_r) = (2, 3, 1, 3)$。
AI (CI翻译)

After these operations, the matrix will be:

执行这些操作后,矩阵将变为:AI (CI翻译)

$\color{green}{-1}$$-31$$\color{green}{6}$$5$$\color{green}{-5}$$\color{green}{20}$$\color{green}{0}$$\color{green}{-20}$$\color{green}{14}$

$\color{green}{-1}$$-31$$\color{green}{6}$$5$$\color{green}{-5}$$\color{green}{20}$$\color{green}{0}$$\color{green}{-20}$$\color{green}{14}$AI (CI翻译)

The seven peaks are highlighted in green. For example:

其中用绿色标出了七个峰值。例如:AI (CI翻译)

  • The cell $(1, 1)$ is a peak because $-1 \ge 5 + 0 - 31 + 6 = -20$;
  • The cell $(3, 1)$ is a peak because $0 \ge -20 + 14 - 1 + 5 = -2$;
  • The cell $(2, 1)$ is not a peak because $5 \lt -5 + 20 - 1 + 0 = 14$.
  • 单元格 $(1, 1)$ 是峰值,因为 $-1 \ge 5 + 0 - 31 + 6 = -20$;
  • 单元格 $(3, 1)$ 是峰值,因为 $0 \ge -20 + 14 - 1 + 5 = -2$;
  • 单元格 $(2, 1)$ 不是峰值,因为 $5 \lt -5 + 20 - 1 + 0 = 14$。
AI (CI翻译)

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

可以证明,少于两次操作无法产生七个峰值,因此答案是 $2$。AI (CI翻译)

In the eighth test case, the only cell has value $-5$. A cell in a $1 \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.

在第八个测试用例中,唯一的单元格的值为 $-5$。对于一个 $1 \times 1$ 的矩阵,当且仅当其值非负时,该单元格是峰值。由于每次操作都会使其值减小,因此不可能使它成为峰值。AI (CI翻译)

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

对于最后一个测试用例,当且仅当两个单元格的值相等时,它们才都是峰值。因此,必须将第一个单元格的值从 $10^9$ 减小到 $-10^9$,这需要 $2 \cdot 10^9$ 次操作。AI (CI翻译)

样例

Input #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
Output #1
2
100000000
1
0
2
734592698
0
-1
0
0
11
6
12
11
7
2000000000
InputCopy OutputCopy
题目信息
题号
2248F
时间限制
2 s
内存限制
256 MB
标签
二分
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。