单调栈 (求最大矩形面积,求最大1的矩形面积)

本文解析了两道经典的算法题,分别是POJ 3494 Largest Submatrix of All 1's 和牛客网638幸运的wnm。通过使用堆栈数据结构和动态规划的方法,文章详细介绍了如何在矩阵中找到全为1的最大子矩阵的面积,提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:https://blog.youkuaiyun.com/hopeztm/article/details/7868581

poj 3494 Largest Submatrix of All 1’s

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <vector>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=2005;
int n,m;
int a[maxn][maxn],b[maxn][maxn]; 
char mapp[maxn][maxn];
struct node
{
	int height;
	int xb;
	node(int a,int b)
	{
		height=a;xb=b;
	}
};
int main()
{
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				scanf("%d",&a[i][j]);
				b[i][j]=a[i][j]==0?0:b[i-1][j]+1;
			}
		}
		int maxi=0;
		stack<node>st;
		for(int i=1;i<=n;i++)
		{
			st.push(node(0,0));
			for(int j=1;j<=m;j++)
			{
				int nh=b[i][j];
				node tp(nh,j);
				while(st.top().height>nh)
				{
					tp=st.top(); 
					st.pop();
					maxi=max(maxi,(j-tp.xb)*tp.height);
				}
				st.push(node(nh,tp.xb));	
			}
			while(!st.empty())
			{
				node tp=st.top();
				st.pop();
				maxi=max(maxi,(m+1-tp.xb)*tp.height);	
			}
		}
		printf("%d\n",maxi);	
	}
	return 0;
}

牛客 638 幸运的wnm

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=1005;
int n,m;
int a[maxn][maxn],b[maxn][maxn];
char mapp[maxn][maxn];
struct node
{
    int height;
    int xb;
    node(int a,int b)
    {
        height=a;xb=b;
    }
};
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                cin>>mapp[i][j];
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                a[i][j]=mapp[i][j]-'0';
            }
        }
        memset(b,0,sizeof(b));
        for(int u=1;u<=n;u++)
        {
            for(int i=1;i<=m;i++)
            {
                if(u!=1&&b[u-1][i]>=u)
                {
                    b[u][i]=b[u-1][i]-1;
                    continue;
                }
                int cnt=0;
                for(int j=u;j<=n;j++)
                {
                    if(a[j][i]==1)
                    {
                        cnt++;
                    }
                    else
                        break;
                }
                b[u][i]=cnt;
            }
        }
        int maxi=0;
        stack<node>st;
        for(int i=1;i<=n;i++)
        {
            st.push(node(0,0));
            for(int j=1;j<=m;j++)
            {
                int nh=b[i][j];
                node tp(nh,j);
                while(st.top().height>nh)
                {
                    tp=st.top();
                    st.pop();
                    maxi=max(maxi,(j-tp.xb)*tp.height);
                }
                st.push(node(nh,tp.xb));   
            }
            while(!st.empty())
            {
                node tp=st.top();
                st.pop();
                maxi=max(maxi,(m+1-tp.xb)*tp.height);  
            }
        }
        cout << maxi << endl;
         
    }
     
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值