POJ 3494 Largest Submatrix of All 1’s

Description

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output

For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input

2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0

Sample Output

0
4 释意:
  给出一个由0,1组合的矩阵,试求出元素最多且都是1的矩形。 poj2559加强版。
题解:
1.将矩阵看成不同高度矩形,若相邻两行中若有1相邻则次矩形的高度便加1,将每一行分别看做矩形的底进行更新,从而问题变成了找面积最大矩形
举例说明:
因为我们要找的是矩形,所以它一定是以 某个行元素开始的,如果枚举行的时候,我们会发现:
对于第一行: 

对于第二行:

第三行:

第四行: 

这样的话,其实我们要找到的某个矩形就转换成 一某一个行开始的 histogram的最大矩形问题了。

那么我们原始矩形可以变成如下的形式的数据:

第一行表示,我们以第一行作为底边,所形成的矩形的高度,其他行也类似。

2.求以第i行为底的最大矩形,利用单调队列或者单调栈,以单调队列为例:

对该行中的每个点的高度为最小基准向左右两边进行松弛,求其可满足的左右区间的最大区间。

以右区间为例:

从该行的右端点开始一次进队遍历,建立单调增的队列将队尾与当前点的高度进行比较,若大于等于,则队尾出队,否则队尾元素的下标便是以当前点高度为最小高度的矩形的右区间最大值+1.

左区间同理亦可得到,从而确定该点所决定的矩形的大小,进而却定以该行为底的最大矩形。

3.代码实现:

 //单调队列实现  C++提交 若用G++则会T!!!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std;
struct Node
{
int val;
int position;
}q[]; // 数组模拟单调递增队列
int n,m;
int l[]; // 以当前点高度为最小高度的矩形的左区间最大值
int r[]; //以当前点高度为最小高度的矩形的右区间最大值
int a[][];
//求每一行的最大矩形
int AREA(int nn)
{
int h = ; //队头
int tail = ;//队尾
//确定以每一个点的高度为最小值的矩形的右区间的最大值
q[].position = n+;
q[].val = -;
for(int i = m;i>=;i--)
{
while(q[tail].val >= a[nn][i]) tail--;
r[i] = q[tail].position-i;
q[++tail].val = a[nn][i];
q[tail].position = i;
}
//确定以每一个点的高度为最小值的矩形的左区间的最大值
h = ;
tail = ;
q[].position = ;
q[].val = -;
for(int i = ;i<=m;i++)
{
while(q[tail].val>=a[nn][i]) tail--;
l[i] = i-q[tail].position;
q[++tail].val = a[nn][i];
q[tail].position = i;
}
//该行中所有矩形的最大值
int max1 = -;
for(int i = ;i<=m;i++)
max1 = max(max1,(l[i]+r[i]-)*a[nn][i]);
return max1;
}
int main()
{ while(~scanf("%d %d",&n,&m))
{
for(int i = ;i<=n;i++)
for(int j = ;j<=m;j++)
scanf("%d",&a[i][j]);
//讲矩阵看做不同高度的矩形进行更新操作
for(int i = ;i<=n;i++)
for(int j = ;j<=m;j++)
if(a[i][j]) a[i][j] = a[i-][j]+a[i][j];
int max1 = -;
//对每行进行遍历确定每行的最大矩形
for(int i = ;i<=n;i++)
max1 = max(AREA(i),max1);
printf("%d\n",max1);
}
return ;
}
 //单调栈实现
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue> using namespace std;
struct re
{
int h;
int p;
}; int m,n;
int a[][];
int h[][];
int l[];
int r[];
int AREA(int R)
{
int i,j;
int max1 = -;
stack<re> s;
//求左区间
re p0,pi;
p0.h = -;
p0.p = -;
s.push(p0);
for(i = ;i<n;i++)
{
pi.p = i;
pi.h = h[R][i];
while(s.top().h>=pi.h) s.pop();
l[i] = i-s.top().p;
s.push(pi);
}
while(!s.empty()) s.pop();
//求右区间
p0.h = -;
p0.p = n;
s.push(p0);
for(i =n- ;i>=;i--)
{
pi.p = i;
pi.h = h[R][i];
while(s.top().h>=pi.h) s.pop();
r[i] = s.top().p-i;
s.push(pi);
}
for(i = ;i<n-;i++)
if((r[i]+l[i]-)*h[R][i]>max1)
max1= (r[i]+l[i]-)*h[R][i];
return max1;
}
int main()
{
int i,j;
while(~scanf("%d%d",&m,&n))
{
for(i = ; i<m; i++)
for(j = ; j<n; j++)
{
scanf("%d",&a[i][j]);
h[i][j] = a[i][j];
}
for(i = ; i<n; i++)
for(j = ; j<m; j++)
if(h[j][i]) h[j][i] += h[j-][i];
int max1 = -;
for(i = ; i<m; i++) max1 = max(max1,AREA(i));
printf("%d\n",max1);
}
}
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.

POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈的更多相关文章

  1. POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵

    Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...

  2. POJ 3494 Largest Submatrix of All 1’s(最大全1子矩阵)

    题目链接:http://poj.org/problem?id=3494 题意:给出一个01的矩阵,找出一个面积最大的全1矩阵. 思路:用h[i][j]表示从位置(i,j)向上连续1的最大长度.之后枚举 ...

  3. POJ 3494 Largest Submatrix of All 1’s

    POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #incl ...

  4. POJ 3494 Largest Submatrix of All 1’s(最大子图形)

    [题目链接] http://poj.org/problem?id=3494 [题目大意] 在01矩阵中求最大全1子矩形 [题解] 在处理每个点的时候,继承上一个点等高度下的左右最大扩展, 计算在该层的 ...

  5. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  6. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  7. POJ-3494 Largest Submatrix of All 1’s (单调栈)

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8551   Ac ...

  8. 第一周任务Largest Submatrix of All 1’s

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9512   Ac ...

  9. Largest Submatrix(动态规划)

    Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. js 生成随机数解决缓存的问题

    对于缓存有一个解决方法是在链接后添加随机数 例如登陆后跳转到链接/home,但是有缓存上次用户的登陆名,于是在/home后面加上一个随机数 var href = '/home?'+Math.rando ...

  2. memcached for .net on windows

    memcached for windowshttp://www.cnblogs.com/dudu/archive/2009/07/19/1526407.htmlhttp://www.cnblogs.c ...

  3. WCF的问题

    使用service调用WCF的时候,有时候会出现 其他信息: HTTP 无法注册 URL 进程不具有此命名空间的访问权限 这样的问题,这时候就需要进行如下尝试: 1,VS的管理权限使用管理员的权限. ...

  4. Java的内存回收机制详解X

    http://blog.csdn.net/yqlakers/article/details/70138786 1 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前 ...

  5. 【洛谷2152】[SDOI2009] SuperGCD(Python好题)

    点此看题面 大致题意: 给你两个长度\(\le10000\)的正整数,让你求它们的\(gcd\). Python​ 高精请绕道. 这题的正解应该是Python. 对于这种高精题,肯定是Python最方 ...

  6. [论文理解]MetaAnchor: Learning to Detect Objects with Customized Anchors

    MetaAnchor: Learning to Detect Objects with Customized Anchors Intro 本文我其实看了几遍也没看懂,看了meta以为是一个很高大上的东 ...

  7. java Socket 客户端服务端对接正确写法(BIO)

    之前在工作中写过一些Socket客户端与服务端的代码,但是当时没有时间仔细研究,只能不报错先过的态度,对其细节了解不深,写的代码有各种问题也浑然不知,只是业务量级以及对接方对接代码没有出现出格的情况所 ...

  8. javaweb基础(26)_jsp标签库开发二

    一.JspFragment类介绍 javax.servlet.jsp.tagext.JspFragment类是在JSP2.0中定义的,它的实例对象代表JSP页面中的一段符合JSP语法规范的JSP片段, ...

  9. JS中进行浮点数计算式,遇到的问题

    今天在做项目时,需要在页面进行计算,但是当两个数都是小数时,计算的结果却不是想象中的: 比如1371.3-0.9算出来却是1370.39999999,后来上网搜一下,原来js是弱类型语言,没有那么高的 ...

  10. Ubuntu使用问题解决办法

    http://blog.csdn.net/ll_0520/article/details/6077913