2019牛客多校第八场 A All-one Matrices

题意:在01矩阵中求极大全1矩阵数量

极大全1矩阵满足以下两个条件:

1.矩阵内元素全部由1构成

2.该矩阵不是其他全1矩阵的子矩阵

题解:枚举每一行,以该行作为矩阵的底,利用单调栈处理出该行上最大全1矩阵,并判断该矩阵有没有可能向下扩展

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 3e3 + 5;
char mp[maxn][maxn];
int h[maxn][maxn];
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%s", mp[i] + 1);
    }
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            if (mp[i][j] == '1')
            {
                h[i][j] = h[i - 1][j] + 1;
            }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        stack<pii> sta;
        int flag = -1;
        for (int j = 1; j <= m + 1; ++j)
        {
            int pos = j;
            while (!sta.empty() && sta.top().first > h[i][j])
            {
                if (sta.top().second <= flag)
                    ans++;
                pos = sta.top().second;
                sta.pop();
            }
            if (!h[i + 1][j])
                flag = j; //判断下面一层是否全1
            if (h[i][j] && (sta.empty() || (sta.top().first < h[i][j])))
                sta.push(pii(h[i][j], pos));
        }
    }
    printf("%d\n", ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/Zeronera/p/11335287.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值