Maximal submatrix
程序员文章站
2022-04-01 13:32:03
...
杭电多校
Problem Description
Given a matrix of n rows and m columns,find the largest area submatrix which is non decreasing on each column
Input
The first line contains an integer T(1≤T≤10)representing the number of test cases.
For each test case, the first line contains two integers n,m(1≤n,m≤2∗103)representing the size of the matrix
the next n line followed. the i-th line contains m integers vij(1≤vij≤5∗103)representing the value of matrix
It is guaranteed that there are no more than 2 testcases with n∗m>10000
Output
For each test case, print a integer representing the Maximal submatrix
Sample Input
1
2 3
1 2 4
2 3 3
Sample Output
4
这里的非递减序列,是将这一列拿出来组成一个数组A,满足
A[i]>=A[i+1];
#include<bits/stdc++.h>
using namespace std;
const int N=5e3;
int a[N][N],b[N][N];
int h[N];
int que[N];
void solve()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
b[i][j]=0;
if(i>1){
b[i][j]=(a[i][j]>=a[i-1][j]);
}
}
}
for(int i=1;i<=m;i++){
h[i]=0;
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(b[i][j]==0){
h[j]=1;
}else{
h[j]++;
}
}
int tot=0;
h[m+1]=0;
for(int j=1;j<=m+1;j++){
while(tot&&h[que[tot]]>h[j]){
ans=max(ans,(j-que[tot-1]-1)*h[que[tot]]);
tot--;
}
que[++tot]=j;
}
}
printf("%d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
solve();
}
}
推荐阅读
-
Largest Submatrix (最大全1子矩阵)
-
leetcode 85. Maximal Rectangle(最大全1子矩阵)
-
LeetCode:Maximal Square浅析
-
LintCode-510. Maximal Rectangle(最大矩形)(单调栈)
-
矩阵中最大矩形 Maximal Rectangle
-
CodeForces 375B Maximum Submatrix 2
-
leetcode-221. Maximal Square 最大正方形
-
Leetcode—221.Maximal Square 最大正方形
-
LeetCode:Maximal Square浅析
-
Maximal submatrix