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
题意:就是给定一个矩阵,让求一个最大子矩阵,并且子矩阵的每列都是非递减的。
解题思路:创建一个dp数组,dp数组里面存的是该数在这一行最大的非递减的个数。然后在每行中进行寻找。
#include<bits/stdc++.h>
using namespace std;
int dp[2222][2222];
int a[2222][2222];
const int inf=0x3f3f3f3f;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
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]);
dp[i][j]=1;
}
for(int i=1;i<=m;i++)
{
for(int j=2;j<=n;j++)
{
if(a[j][i]>=a[j-1][i])
dp[j][i]=dp[j-1][i]+1;
}
}//初始化dp数组
int maxx=-1;
for(int i=1;i<=n;i++)
{
int k=1;
int s=0;
int maxn=inf;
while(k<=m)
{
if(dp[i][k]==1)
{
s=0;
maxn=inf;
}
else
{
maxn=min(maxn,dp[i][k]);
s++;
maxx=max(maxx,maxn*s);
}
k++;
}
}
maxx=max(maxx,m);//对一行进行比较,因为一行也是一个矩形
printf("%d\n",maxx);
}
}