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

来源:互联网 发布:汉仪菱心简体mac版 编辑:程序博客网 时间:2024/05/18 02:51

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 1005int 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;    }}