Maximal submatrix
Description
Given a matrix of nn rows and mm columns,find the largest area submatrix which is non decreasing on each column
Input
The first line contains an integer T(1≤T≤10)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∗10^3)n,m(1≤n,m≤2∗10
Sample Input 1
1
2 3
1 2 4
2 3 3
Sample Output 1
4
#include<bits/stdc++.h>
using namespace std;
#define int long long
int l[2010][2010],r[2010][2010],h[2010][2010];
int a[2010][2010];
int duanceng[2010][2010];
int row_dealed[2010];
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int T,n,m;
cin>>T;
while(T--){
cin>>n>>m;
int ans=m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
l[i][j]=0;
r[i][j]=0;
h[i][j]=0;
duanceng[i][j]=0;
//row_dealed[j]=0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]>=a[i-1][j])h[i][j]=h[i-1][j]+1;
else duanceng[i][j]=1;
}
}
for(int j=1;j<=m;j++){//一列一列的查看是否断层
for(int i=1;i<=n;i++){
if(duanceng[i][j]){
for(int k=i;k<=n;k++)duanceng[j][k]=1;//第j列第k行之后的全断层
break;//直接break掉
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(h[i][j]&&h[i][j-1])l[i][j]=l[i][j-1]+1;
if(h[i][m-j+1]&&h[i][m-j+2])r[i][m-j+1]=r[i][m-j+2]+1;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(h[i][j]>=2)
l[i][j]=min(l[i][j],l[i-1][j]),
r[i][j]=min(r[i][j],r[i-1][j]);
if(duanceng[i][j])ans=max(ans , (l[i][j]+r[i][j]+1)*(h[i][j]+1));
else ans=max(ans , (l[i][j]+r[i][j]+1)*h[i][j]);
}
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++)cout<<h[i][j]<<" ";
// cout<<endl;
// }
// cout<<"============================"<<endl;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++)cout<<duanceng[i][j]<<" ";
// cout<<endl;
// }
// cout<<"============================"<<endl;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++)cout<<l[i][j]<<" ";
// cout<<endl;
// }
// cout<<"============================"<<endl;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++)cout<<r[i][j]<<" ";
// cout<<endl;
// }
cout<<ans<<endl;
}
}
我tm****************做了这么久你卡我内存?