CF2248F.Matrix Elimination
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 vi,j.
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
v_x,ygesum_substack1leilenineqxv_i,y+sum_substack1lejlemjneqyv_x,j.
You may perform the following operation any number of times (possibly zero):
- Choose four integers xl, xr, yl, and yr (1≤xl≤xr≤n, 1≤yl≤yr≤m).
- Subtract 1 from vi,j for every xl≤i≤xr and yl≤j≤yr.
Find the minimum number of operations required to obtain a matrix with at least k peaks.
给你一个整数 k 和一个具有 n 行 m 列的矩阵 v。第 i 行第 j 列的元素记为 vi,j。
若某个单元格 (x,y) 的值大于或等于其所在行 x 和列 y 中其余所有单元格的值之和,则称该单元格 (x,y) 为峰点。形式化地,(x,y) 是一个峰点当且仅当
vx,y≥1≤i≤ni=x∑vi,y+1≤j≤mj=y∑vx,j.
你可以执行以下操作任意多次(包括零次):
- 选择四个整数 xl、xr、yl 和 yr(满足 1≤xl≤xr≤n,1≤yl≤yr≤m);
- 对所有满足 xl≤i≤xr 且 yl≤j≤yr 的单元格 (i,j),将 vi,j 减 1。
求使矩阵中至少包含 k 个峰点所需的最少操作次数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains three integers n, m, and k (1≤n,m≤105, 1≤k≤n⋅m, n⋅m≤105) — the number of rows, the number of columns, and the required number of peaks.
The i-th of the next n lines contains m integers vi,1,vi,2,…,vi,m (−109≤vi,j≤109).
It is guaranteed that the sum of n⋅m over all test cases does not exceed 105.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含三个整数 n、m 和 k(1≤n,m≤105,1≤k≤n⋅m,且 n⋅m≤105),分别表示行数、列数以及所需的峰的数量。
接下来的 n 行中,第 i 行包含 m 个整数 vi,1,vi,2,…,vi,m(−109≤vi,j≤109)。
保证所有测试用例中 n⋅m 的总和不超过 105。
输出格式
For each test case, output a single integer — the minimum number of operations required to obtain at least k peaks.
If there is no solution, print a single integer −1.
对于每个测试用例,输出一个整数——得到至少 k 个峰值所需的最少操作次数。
若无解,则输出整数 −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);
- choose (xl,xr,yl,yr)=(2,3,1,3).
After these operations, the matrix will be:
−1
−31
6
5
−5
20
0
−20
14
The seven peaks are highlighted in green. For example:
- The cell (1,1) is a peak because −1≥5+0−31+6=−20;
- The cell (3,1) is a peak because 0≥−20+14−1+5=−2;
- The cell (2,1) is not a peak because 5<−5+20−1+0=14.
It can be shown that fewer than two operations cannot create seven peaks, so the answer is 2.
In the eighth test case, the only cell has value −5. A cell in a 1×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 109 to −109, which requires 2⋅109 operations.
对于第一个测试用例,你可以执行以下两次操作:
- 选择 (xl,xr,yl,yr)=(1,3,2,3);
- 选择 (xl,xr,yl,yr)=(2,3,1,3)。
执行这些操作后,矩阵将变为:
−1
−31
6
5
−5
20
0
−20
14
其中七个峰值单元格以绿色高亮显示。例如:
- 单元格 (1,1) 是一个峰值,因为 −1≥5+0−31+6=−20;
- 单元格 (3,1) 是一个峰值,因为 0≥−20+14−1+5=−2;
- 单元格 (2,1) 不是峰值,因为 5<−5+20−1+0=14。
可以证明:少于两次操作无法构造出七个峰值,因此答案为 2。
在第八个测试用例中,矩阵仅含一个单元格,其值为 −5。在 1×1 矩阵中,一个单元格是峰值当且仅当其值非负。由于每次操作只会降低该值,因此不可能使其成为峰值。
在最后一个测试用例中,两个单元格均为峰值当且仅当它们的值相等。因此,第一个单元格必须从 109 减少至 −109,这需要 2⋅109 次操作。
输入解题思路,AI测评打分。不知道怎么写?