题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158

基准时间限制: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

题解:

POJ2559加强版,预处理一下以每个格子作为底,最多可以往上延伸多少个格子,这样就可以把这些连续的格子看成一根柱子,如果按行枚举,那么就转化成了POJ2559的问题了,即利用单调栈,当然这题也可以直接暴力枚举。

暴力枚举:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 5e2+; int a[MAXN][MAXN];
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]) a[i][j] += a[i-][j];
} int ans = ;
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
int w = a[i][j];
ans = max(ans, w*);
for(int k = j+; k<=m; k++)
{
w = min(w,a[i][k]);
ans = max(ans, w*(k-j+));
}
}
printf("%d\n", ans);
}
}

单调栈:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 5e2+; int h[MAXN][MAXN];
int Stack[MAXN][], top;
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
scanf("%d",&h[i][j]);
if(h[i][j]) h[i][j] += h[i-][j]; //预处理出高度,这样就可转化为POJ2559了
} int ans = ;
for(int i = ; i<=n; i++)
{
top = ;
h[i][m+] = -; //在最后加个很小的值,就能把栈里的元素全部倒出来了
for(int j = ; j<=m+; j++)
{
int len = ; //长度
while(top && Stack[top-][]>=h[i][j]) //如果当前高度小于等于
{
len += Stack[top-][]; //长度累加
ans = max(ans,Stack[top-][]*len);
top--;
}
Stack[top][] = h[i][j]; //当前元素入栈
Stack[top++][] = len+; //把出栈了的元素的长度都累加到入栈元素当中去
}
} printf("%d\n", ans);
}
}

51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈的更多相关文章

  1. 51nod 1158 全是1的最大子矩阵

    题目链接:51nod 1158 全是1的最大子矩阵 题目分类是单调栈,我这里直接用与解最大子矩阵类似的办法水过了... #include<cstdio> #include<cstri ...

  2. HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)

    单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...

  3. 51nod 1158 全是1的最大子矩阵(单调栈 ,o(n*m))

    前置问题:51nod 1102 面积最大的矩形 附上链接: 51nod 1102 面积最大的矩形 这题的题解博客 需要了解的知识:单调栈,在前置问题中已经讲解. 解题思路 对每行求左边连续1的个数,得 ...

  4. 51Nod 1116 K进制下的大数(暴力枚举)

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...

  5. 51nod1158 全是1的最大子矩阵

    跟最大子矩阵差不多O(n3)扫一下.有更优写法?挖坑! #include<cstdio> #include<cstring> #include<cctype> #i ...

  6. 51nod 1102 面积最大的矩形 (单调栈)

    链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 思路: 首先介绍下单调栈的功能:利用单调栈,可以找到从左/ ...

  7. 单调栈求全1(或全0)子矩阵的个数 洛谷P5300与或和 P3400仓鼠窝

    爆零好爽,被中学生虐好爽,还好我毕业得早 求全1(或全0)子矩阵的个数,看了题解有好几种思路,我学了三种,但有两种不是很理解,而且也没另外那个跑得快,所以简单讲述一一下我会的那种来自Caro23333 ...

  8. 51nod 1437 迈克步(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...

  9. 51nod 1102 面积最大的矩形(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 题意: 思路: 做法就是求出每个长方形向左向右所能延伸的最大距离. ...

随机推荐

  1. foreach_break 面试记录

    版权所有@foreach_break] [博客地址 http://www.cnblogs.com/foreach-break] 可以转载,但必须注明出处并保持博客超链接 背景 自从2013年离开北京后 ...

  2. xshell的快捷键(很有用)

    删除 ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dl ctrl + h      删除光标所在位置前的字符相当于VIM里hx或者dh ctrl + k      删除光标 ...

  3. Android常用资源

    Eclipse ADT http://developer.android.com/sdk/installing/installing-adt.html https://dl-ssl.google.co ...

  4. iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客

    近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang  新浪微博 指示根视图: se ...

  5. g2o 初始化

    typedef g2o::BlockSolver< g2o::BlockSolverTraits<,> > Block; // pose 维度为 6, landmark 维度为 ...

  6. centos-64整合nginx和tomcat

    centos-64整合nginx和tomcat 分类: Linux 2013-04-25 10:41 128人阅读 评论(0) 收藏 举报 1.安装wget和依赖包 yum install wget ...

  7. opencl教程

    http://www.altera.com.cn/corporate/news_room/releases/2013/products/nr-opencl-sdk-13.0.html http://w ...

  8. opencv3.3.1 opencv_contribut 3.3.1 git 20180117最新版的在ubuntu1604上的编译

    过程: 1.  git  clone  ...   contribut 2. git  clone  ...  opencv 3.  git  checkout  -b     v3.3.1 4 gi ...

  9. js 显示当前系统时间

    <div id="test"></div>                <script >                    setInt ...

  10. Visual Studio 2017 for Mac Preview

    Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...