Maximal submatrix

杭电多校
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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值