POJ3494--Largest Submatrix of All 1's(二维栈)

题目大意:给出一个m*n的01矩阵,求由1组成的最大矩形


分析:这题也是栈的运用,只不过,现在是二维的,所以,我们要先转换成一维。

首先,我们可以一排一排的计算,而每一排每一个矩形的宽都是1,若改点为0,则没有高。若为1,则该矩形的高就是当前的1加上它这一列的上方连续的1的个数。这样子,我们就化二维为一维了。之后的计算就和POJ2559差不多了,传送门http://blog.csdn.net/hhhhhhj123/article/details/47790801


代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 2222;

struct Rect {
    int a, h, l, r;
}rect[maxn][maxn];
int st[maxn];
int m, n;


int main() {
    while(~scanf("%d%d", &m, &n)) {
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++) {
                scanf("%d", &rect[i][j].a);
                if(i == 0 || rect[i][j].a == 0) rect[i][j].h = rect[i][j].a;
                else rect[i][j].h = rect[i-1][j].h+rect[i][j].a;
                rect[i][j].l = rect[i][j].r = j;
            }
        int t;
        for(int i = 1; i < m; i++) {
            t = 0;
            for(int j = 0; j < n; j++) {
                while(t > 0 && rect[i][st[t-1]].h >= rect[i][j].h) t--;
                rect[i][j].l = t == 0 ? 0 : (st[t-1]+1);
                st[t++] = j;
            }
        }
        for(int i = 1; i < m; i++) {
            t = 0;
            for(int j = n-1; j >= 0; j--) {
                while(t > 0 && rect[i][st[t-1]].h >= rect[i][j].h) t--;
                rect[i][j].r = t == 0 ? n-1 : (st[t-1]-1);
                st[t++] = j;
            }
        }
        int ans = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                ans = max(ans, rect[i][j].h*(rect[i][j].r-rect[i][j].l+1));
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值