2021-08-01

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****************做了这么久你卡我内存?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值