You have written many programs to search mazes so matrix search shouldn't be any different, or will it?
An integer matrix with R rows and C columns has
sub matrices. We want to select a sub matrix with sum (the sum of all integers in it) greater than or equal to a given integer S. We want the size of the sub matrix to be the least possible. The size of a sub matrix is defined as the number of elements in that sub matrix (i.e., number of rows * number of columns in that sub matrix).
The first input line consists of three integers R, C (1 ≤ R ≤ 100,000; 1 ≤ C ≤ 100,000;1 ≤ R*C ≤ 100,000) and S. Next R lines contain the description of the matrix. Each of these R lines contains C integers separated by a single space. All integers (other than R and C) are between -10^9 and +10^9, inclusive.
Print the size of the minimum sub matrix whose sum is greater or equal to the given S. If there is no such sub matrix, output -1.
3 3 26
1 2 3
4 5 6
7 8 9
4
3 3 0
-1 -2 -3
-4 -5 -6
-7 -8 -9
-1
2 2 1
-1 -2
0 2
1
题目大意:
给出一个大小为 R×C 的矩阵,要求选出一个子矩阵,在子矩阵和不小于 S 的条件下,输出子矩阵的最小大小。
分析:
涉及到二维子矩阵的元素和,先降维。枚举起始行 i 和结束行 j ,将 i 到 j 之间的行按列压缩到一维,则对于这个一维数组,问题转化为找到一对 l 和 r ,使得 r-l+1 最小,并且 l 到 r 之间的元素和大于等于 S 。
对一维数组求前缀和,由于这里有负数元素,前缀和会减小,所以用单调栈记录递增的前缀和,对于每个位置 k ,二分单调栈找到第一个小于等于 num[k]-S 的前缀和 num[x] ,则 x 到 k 之间的元素和满足大于等于 S ,并且 x 是满足该条件的最大下标。
具体解释见代码。
#include
#include
#include
#include
#include
#include
#include
#include