小米邀请赛补题

博客围绕牛客网J题“Matrix Subtraction”展开,给定矩阵及子矩阵大小,判断能否通过重复选择子矩阵并减1使矩阵元素全为零。题解采用贪心算法,从左上角元素开始操作,为避免复杂度爆炸,使用二维差分数据结构维护,枚举元素时判断是否合法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

J :Matrix Subtraction

链接:https://ac.nowcoder.com/acm/contest/7501/J
来源:牛客网

Given a matrix M of size n\times mn×m and two integers a,b , determine weither it is possible to make all entrys of M

zero by repeatedly choosing a×b submatrices and reduce the values in the chosen matrices by 1. If possible, print “_” in one line, or print “QAQ” in one line.
输入描述:
The first line contains one integer T~(1\le T \le 100)T (1≤T≤100), denoting the number of test cases.

For each test case:

The first line contains four integers n,m,a,b~(1\le n,m \le 1000, 1\le a\le n, 1\le b\le m)n,m,a,b (1≤n,m≤1000,1≤a≤n,1≤b≤m), denoting the size of given matrix and the size of chosen submatrices respectively.

The next n_{}n

lines each contains m_{}m

integers M_{i,j}~(0\le M_{i,j} \le 10^9)M i,j (0≤M i,j ≤10 9 ), denoting the entrys of matrix M_{}M

.

It’s guaranteed that \sum nm \le 10^6∑nm≤10
6
.
输出描述:
Print T_{}T

lines each containing a string “_” or “QAQ”, denoting the answer to each test case.
示例1
输入
复制
2
2 2 1 2
1 2
1 2
2 3 1 2
1 2 1
1 2 1
输出
复制
QAQ
_
说明
For the second case, one possible scheme is to choose (1, 1) - (1, 2), (1, 2) - (1, 3), (2, 1) - (2, 2), (2, 2) - (2, 3)_{}(1,1)−(1,2),(1,2)−(1,3),(2,1)−(2,2),(2,2)−(2,3)

respectively.

题解:首先贪心,我们看最左上角的元素,如果左上角,那么我们就要对其子矩阵的所有元素减去他,然后因为左上角已经是0了,假如右边存在的话,我们就只能让它成为子矩阵的左上角然后再对所有子矩阵减去右边,以此类推,但是直接暴力的话复杂度会炸,我们需要用数据结构来维护,直接用二维差分即可,每次枚举到某个元素的时候判断它是否不小于0,如果不是就不合法.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值