Maximal submatrix(hdu2021第一场)
题目链接:
![Alt](https://acm.hdu.edu.cn/showproblem.php?pid=6957)
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
题意即为给定了一个n行m列的矩阵,求每列不递减的最大面积子矩阵。
b
[
i
]
[
j
]
=
1
b[i][j]=1
b[i][j]=1即是满足条件可取,
x
[
j
]
x[j]
x[j]则是记录在第
j
j
j列有多少符合条件的连续,然后不断比较更新得到以
x
[
c
[
c
n
t
]
]
x[c[cnt]]
x[c[cnt]]为宽,
(
j
−
1
−
c
[
c
n
t
−
1
]
)
(j-1-c[cnt-1])
(j−1−c[cnt−1])为长的矩阵面积.
补这道题还遇到了RE(AV),原因可能是
c
i
n
cin
cin改
s
c
a
n
f
scanf
scanf忘记加上取地址符了而非数组大小。
题解:
#include<bits/stdc++.h>
using namespace std;
long long a[2010][2010],x[2000010],c[2000010];
bool b[2010][2010];
int main()
{
int t;
cin>>t;
while(t--)
{
// memset(b,0,sizeof(b));
//memset(x,0,sizeof(x));
int n,m;
long long ans=0;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%lld",&a[i][j]);
if(a[i][j]>=a[i-1][j])
{
b[i][j]=1;
}
else b[i][j]=0;
}
}
for(int i=1;i<=m;i++)x[i]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(b[i][j]!=0)x[j]++;
else x[j]=1;
}
x[m+1]=0;
int cnt=0;
for(int j=1;j<=m+1;j++)
{
while(cnt&&(x[c[cnt]]>x[j]))
{
long long ans1=(j-1-c[cnt-1])*x[c[cnt]];
ans=max(ans,ans1),cnt--;
}
c[++cnt]=j;
}
}
cout<<ans<<endl;
}
}