2019牛客多校第二场 H Second Large Rectangle

题目链接:https://ac.nowcoder.com/acm/contest/882/H

最大全零子矩阵扩展题

先一遍单调栈记录某一个最大的矩阵

再一遍单调栈,如果当前最大的矩阵长宽为h,len,且坐标与记录的最大矩阵的坐标不同,ans=max(h*len,ans)

不然就只更新h-1和len-1的情况

#include<bits/stdc++.h>
#define maxl 1010
 
using namespace std;
 
int n,m,top,mx,mx1,my1,mx2,my2,secmx;
int s[maxl],h[maxl],l[maxl],r[maxl];
int a[maxl][maxl];
char ch[maxl];
 
inline void prework()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",ch+1);
        for(int j=1;j<=m;j++)
            a[i][j]=ch[j]-'0';
    }
}
 
inline void solve1()
{
    mx=0;int tmp;
    for(int i=1;i<=m;i++)
        h[i]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        if(a[i][j]==1)
            h[j]++;
        else
            h[j]=0;
        top=0;
        for(int j=1;j<=m;j++)
        {
            while(top>0 && h[s[top]]>h[j])
                r[s[top]]=j-1,--top;
            l[j]=s[top]+1;
            s[++top]=j;
        }
        while(top>0)
            r[s[top]]=m,--top;
        for(int j=1;j<=m;j++)
        if(h[j])
        {
            tmp=h[j]*(r[j]-l[j]+1);
            if(tmp>mx)
            {
                mx=tmp;
                mx1=i-h[j]+1;my1=l[j];
                mx2=i;my2=r[j];
            }
        }
    }
}
 
inline void solve2()
{
    secmx=0;int tmp,tx1,tx2,ty1,ty2;
    for(int i=1;i<=m;i++)
        h[i]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        if(a[i][j]==1)
            h[j]++;
        else
            h[j]=0;
        top=0;
        for(int j=1;j<=m;j++)
        {
            while(top>0 && h[s[top]]>h[j])
                r[s[top]]=j-1,--top;
            l[j]=s[top]+1;
            s[++top]=j;
        }
        while(top>0)
            r[s[top]]=m,--top;
        for(int j=1;j<=m;j++)
        if(h[j])
        {
            tx1=i-h[j]+1;ty1=l[j];
            tx2=i;ty2=r[j];
            tmp=h[j]*(r[j]-l[j]+1);
            if(mx1!=tx1 || my1!=ty1 || mx2!=tx2 || my2!=ty2)
                secmx=max(tmp,secmx);
            secmx=max(secmx,(h[j]-1)*(r[j]-l[j]+1));
            secmx=max(secmx,h[j]*(r[j]-l[j]));
        }
    }
}
 
inline void mainwork()
{
    solve1();
    solve2();
}
 
inline void print()
{
    printf("%d\n",secmx);
}
 
int main()
{
    prework();
    mainwork();
    print();
    return 0;  
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值