Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 8551   Accepted: 3089
Case Time Limit: 2000MS

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 ≤ mn ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with nnumbers. 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

题意:

求内部全部都是1的子矩阵最大面积。

思路:

第一时间想到了一个n的3次方的dp,但是这样肯定超时。

类似的题 :https://www.cnblogs.com/ZGQblogs/p/10664506.html

如果不是在学单调栈,我觉得我一定是想不到的。

首先就是维护每一行的每一个位置,如果以当前行为底,上面连续的1有多少个。

然后就是这个题:http://poj.org/problem?id=2559

代码:

加了读入挂才过~~~

 #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int l[maxn],r[maxn];
struct node{
int num,pos;
};
int mp[maxn][maxn];
stack<node>st; int solve(int *num,int n){
for(int i=;i<=n;i++){
l[i]=r[i]=i;
}
num[]=num[n+]=-;
for(int i=;i<=n+;i++){
while(!st.empty()&&st.top().num>num[i]){
r[st.top().pos]=i-;
st.pop();
}
st.push(node{num[i],i});
}
while(!st.empty()){st.pop();} for(int i=n;i>=;i--){
while(!st.empty()&&st.top().num>num[i]){
l[st.top().pos]=i+;
st.pop();
}
st.push(node{num[i],i});
}
ll ans=;
for(int i=;i<=n;i++){
ans=max(ans,1ll*num[i]*(r[i]-l[i]+));
}
while(!st.empty()){st.pop();}
return ans;
} char buf[maxn], *ps = buf, *pe = buf+;
inline void rnext(){
if(++ps == pe)
pe = (ps = buf)+fread(buf,,sizeof(buf),stdin);
}
template <class T>
inline bool in(T &ans)
{
ans = ;
T f = ;
if(ps == pe) return false;
do{
rnext();
if('-' == *ps) f = -;
}while(!isdigit(*ps) && ps != pe);
if(ps == pe) return false;
do
{
ans = (ans<<)+(ans<<)+*ps-;
rnext();
}while(isdigit(*ps) && ps != pe);
ans *= f;
return true;
} int main()
{
// freopen("in.txt","r",stdin);
int n,m;
while(true){
in(n);in(m);
if(!m||!n){break;}
int ans=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
in(mp[i][j]);
// cout<<mp[i][j]<<endl;
if(mp[i][j]==){mp[i][j]+=mp[i-][j];}
}
ans=max(ans,solve(mp[i],m));
}
printf("%d\n",ans);
}
return ;
}

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 单调队列||单调栈

    POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...

  3. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  4. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

  5. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

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

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

  8. POJ 3494 Largest Submatrix of All 1’s

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

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

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

  10. POJ3494Largest Submatrix of All 1’s[单调栈]

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

随机推荐

  1. Nginx日志常用统计分析命令

    IP相关统计 统计IP访问量(独立ip访问数量) awk '{print $1}' access.log | sort -n | uniq | wc -l 查看某一时间段的IP访问量(4-5点) gr ...

  2. Flask技术问题汇总

    1:Flask 使用 request对象代理了当前请求的上下文.这么做什么好处和坏处? 好处:flask封装了C端发起request对象,这样就可以使用上下文临时把某些对象变为全局可访问:如果不封装, ...

  3. Lua rawget rawset newindex 函数定义和例子

    在绝大多数情况下,我们都不会用到rawget和rawset. 本文的运行环境:lua 5.3 for windows rawset 赋值操作 rawset是在设置值的过程,进行处理,比如:当某个值改变 ...

  4. 西湖论剑2019复现-Web之首家线上赌场上线啦

    首页打开 经过测试发现name和code参数可控,但尝试注入没有发现注入点,于是直接扫描目录找思路 一扫描,果然有问题 目录扫描里面可以看到有一个/.DS_Store的文件,DS_Store是Mac ...

  5. mysql8.0绿色版安装及mysqldump备份

    1.下载mysql绿色版压缩包https://dev.mysql.com/downloads/mysql/ 2.解压到安装目录后,在根目录创建data文件夹 3.把mysql下的bin目录添加到环境变 ...

  6. wordpress如何利用插件添加优酷土豆等视频到自己的博客上

    wordpress有时候需要添加优酷.土豆等网站的视频到自己的博客上,传统的分享方法不能符合电脑端和手机端屏幕大小的需求,又比较繁琐,怎样利用插件的方法进行添加呢,本视频向你介绍一款这样的插件——Sm ...

  7. 如莲开发平台(MIS基础框架、Java技术、B/S结构)

    关于     「如莲」是一套MIS类系统基础框架,主要用于各类“管理信息系统”的开发,也适合做网站后台开发.可省去开发时的框架搭建.规范约定.权限管理等基础工作,直接专注于业务功能实现.     「如 ...

  8. git tag 打标签

    注意:在哪个分支上打tag一定要先提交该分支到远程gitlab仓库 标签(tag)操作 1. 查看所有标签 git tag 默认标签是打在最新提交的commit上的 2.本地打新标签 git tag ...

  9. mybatis insertUseGeneratedKeys 返回主键为null

    package tk.mybatis.mapper.common.special; import org.apache.ibatis.annotations.InsertProvider; impor ...

  10. protobuf使用详解

    https://blog.csdn.net/skh2015java/article/details/78404235 原文地址:http://blog.csdn.net/lyjshen/article ...