51 nod 1158 全是1的最大子矩阵(单调栈)

基准时间限制:1 秒 空间限制:131072 KB 分值: 80  难度:5级算法题
 收藏
 关注
给出1个M*N的矩阵M1,里面的元素只有0或1,找出M1的一个子矩阵M2,M2中的元素只有1,并且M2的面积是最大的。输出M2的面积。
Input
第1行:2个数m,n中间用空格分隔(2 <= m,n <= 500)
第2 - N + 1行:每行m个数,中间用空格分隔,均为0或1。
Output
输出最大全是1的子矩阵的面积。
Input示例
3 3
1 1 0
1 1 1
0 1 1
Output示例
4


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
char str[1100][1100];
int sum[1100][1100], d[1010][1010];
struct node
{
    int id, x;
};
stack<node>st;

int main()
{
    int n, m, x;
    scanf("%d %d", &n, &m);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            scanf("%d", &x);
            d[i][j]=x;
        }
    }
    int ans=0;
    memset(sum,0,sizeof(sum));
    for(int i=1; i<=n; i++)
    {
        if(i==6)
        {
            int h=1;

        }
        while(!st.empty()) st.pop();
        for(int j=1; j<=m+1; j++)
        {
            if(d[i][j]) sum[i][j]=sum[i-1][j]+1;
            int id=-1;
            while(!st.empty()&&st.top().x>sum[i][j])
            {
                ans=max(ans,st.top().x*(j-st.top().id));
                id=st.top().id;
                st.pop();
            }
            if(st.empty()) ans=max(ans,sum[i][j]*j);
            if(id==-1) id=j;
            st.push((node)
            {
                id,sum[i][j]
            });
        }
    }
    cout<<ans<<endl;
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值