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. linux命令 ——目录

    开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.http://codingstan ...

  2. Python风格规范-FYI

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  3. fopen, fdopen, freopen - 打开流

    SYNOPSIS (总览) #include <stdio.h> FILE *fopen(const char *path, const char *mode); FILE *fdopen ...

  4. python实现二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  5. python读取excel中的数据

    import numpy as np import matplotlib.pyplot as plt import pandas as pd #df = pd.read_excel('/Users/N ...

  6. React后台管理系统- rc-pagination分页组件封装

    1.用户列表页面使用的rc-pagination分页组件 Github地址: https://github.com/react-component/pagination 2.安装 cnpm insta ...

  7. Java如何将十六进制数转换为十进制数的自编程序

    package com.swift;//所属包 import java.util.Scanner;//导入扫描器 public class Hex2Decimal { public static vo ...

  8. Jquery的简单API

    dsfsdjgsdjgsdjkg <script>console.log('erftwet')</script>

  9. XML字符串解析

    不多说,直接上代码: import java.io.StringReader; import org.dom4j.Document; import org.dom4j.DocumentExceptio ...

  10. Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本

    Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/92234 ...