bz入门oj 1457: 最大全零矩形

http://begin.lydsy.com/JudgeOnline/problem.php?id=1457

1457: 最大全零矩形
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 18 Solved: 4
[Submit][Status][Web Board]
Description
给你一个N*M(N,M<=1000)的01矩形,求一个面积最大的不包含数字1的矩形。
Input
第一行两个数N,M。
接下来N行,每行M个数为0或1。
Output
一个数ans表示最大空矩形的面积。
Sample Input
2 4
1 0 0 0
0 1 1 0
Sample Output
3

很神奇的做法,看着别人的博客懂得 。
http://blog.csdn.net/qq_35264769/article/details/53066287
但是理解的方式不一样
下面循环里面的东西,让我觉得,思维真的非常重要。
和kmp非常相似。把一个n方的东西变成了线性的复杂度 ,

做的思维就是合并。
把一列合并的最宽。
枚举每列 向两边扩展 。这里用到了一个我认为类似kmp的东西。。

#include <bits/stdc++.h>
using namespace std;
#define mo 1005
int d[mo];
int a[mo][mo];
int l[mo],h[mo];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>a[i][j];

        memset(d,0,sizeof(d));
        int maxs=0;
        for(int i=1;i<=n;i++)
        {
            //cout<<1<<endl;
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==0) d[j]++;
                else d[j]=0;
               // cout<<d[j]<<' ';
            }
            //cout<<endl<<endl;;

            for(int j=1;j<=m;j++)
            {
                l[j]=j;
                while(l[j]>1&&d[j]<=d[l[j]-1])
                {
                    l[j]=l[l[j]-1];
                    //cout<<l[j]<<' '<<j<<endl;
                }
                //cout<<l[j]<<' '<<"x "<<endl;
            }
            //cout<<endl;
            for(int j=m;j>0;j--)
            {
                h[j]=j;
                while(h[j]<m&&d[j]<=d[h[j]+1])
                {
                    h[j]=h[h[j]+1];
                }
                //cout<<h[j]<<' ';
            }
           // cout<<endl<<endl;
            for(int j=1;j<=m;j++)
            {

                maxs=max(maxs,(d[j]*(h[j]-l[j]+1)));
               // cout<<d[j]<<' '<<l[j]<<' '<<h[j]<<endl;
            }

        }
        cout<<maxs<<endl;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值