• 最大全1子矩阵模板+全1矩阵数量模板


    题目链接

    题目思路

    最大全1子矩阵模板题

    用单调栈预处某个点左边和右边比它本身大的范围即可

    假设(h[i])为矩阵高度即可,然后计算即可

    代码

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define debug cout<<"I AM HERE"<<endl;
    using namespace std;
    typedef long long ll;
    const int maxn=100000+5,inf=0x3f3f3f3f,mod=1e9+7;
    const double eps=1e-6;
    int n,m;
    int h[maxn];
    int l[maxn],r[maxn];
    signed main(){
        while(scanf("%d",&n)!=-1&&n){
            for(int i=1;i<=n;i++){
                scanf("%d",&h[i]);
            }
            ll ans=0;
            stack<int> sta;
            for(int j=1;j<=n;j++){
                while(!sta.empty()&&h[sta.top()]>=h[j]){
                    sta.pop();
                }
                l[j]=sta.empty()?0:sta.top();
                sta.push(j);
            }
            while(!sta.empty()) sta.pop();
            for(int j=n;j>=1;j--){
                 while(!sta.empty()&&h[sta.top()]>=h[j]){
                    sta.pop();
                }
                r[j]=sta.empty()?n+1:sta.top();
                sta.push(j);
            }
            for(int j=1;j<=n;j++){
                ans=max(ans,1ll*(r[j]-l[j]-1)*h[j]);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
    
    

    题目链接

    思路

    全1矩阵数量模板题

    也是用单调栈,然后加dp的思维,可以自己去领悟

    (dp[i][j])表示以((i,j))为右下角的方案数

    代码

    class Solution {
    public:
        const int maxn=2e2+5;
        int h[maxn][maxn];
        int l[maxn];
        int dp[maxn];
        int numSubmat(vector<vector<int>>& mat) {
            int n=mat.size();
            int m=mat[1].size();
            for(int j=1;j<=m;j++){
                for(int i=1;i<=n;i++){
                    if(mat[i-1][j-1]){
                        h[i][j]=0;
                    }else{
                        h[i][j]=h[i-1][j]+1;
                    }
                }
            }
            int ans=0;
            for(int i=1;i<=n;i++){
                stack<int> sta;
                for(int j=1;j<=m;j++){
                    while(!sta.empty()&&h[i][sta.top()]>=h[i][j]){
                        sta.pop();
                    }
                    l[j]=sta.empty()?0:sta.top();
                    sta.push(j);
                    dp[j]=(j-l[j])*h[i][j]+dp[l[j]];
                    ans=ans+dp[j];
                }
            }
            return ans;
        }
    
    };
    
    
    不摆烂了,写题
  • 相关阅读:
    go——数组
    go——流程控制
    go——基本类型
    go——基本构成要素
    go——常量
    go——变量
    go——标准命令
    go——工程结构
    python 优雅的使用正则表达式 ~ 1
    python 安装操作 MySQL 数据库.
  • 原文地址:https://www.cnblogs.com/hunxuewangzi/p/15149797.html
Copyright © 2020-2023  润新知