POJ-3494 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 ≤ m, n ≤ 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 (单调栈)的更多相关文章
- 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 ...
- 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 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
- 题解报告: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 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
- POJ 3494 Largest Submatrix of All 1’s(最大全1子矩阵)
题目链接:http://poj.org/problem?id=3494 题意:给出一个01的矩阵,找出一个面积最大的全1矩阵. 思路:用h[i][j]表示从位置(i,j)向上连续1的最大长度.之后枚举 ...
- POJ 3494 Largest Submatrix of All 1’s
POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #incl ...
- POJ 3494 Largest Submatrix of All 1’s(最大子图形)
[题目链接] http://poj.org/problem?id=3494 [题目大意] 在01矩阵中求最大全1子矩形 [题解] 在处理每个点的时候,继承上一个点等高度下的左右最大扩展, 计算在该层的 ...
- POJ3494Largest Submatrix of All 1’s[单调栈]
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 5883 Ac ...
随机推荐
- CSS3文字与字体 text-overflow 与 word-wrap
text-overflow 对象内的文本溢出部分采用省略“...”标记 或者 剪切: text-overflow:elip(超出容器边界的内容剪切掉) | ellipsis(超出容器边界内容省略标示 ...
- java基础(一):谈谈java内存管理与垃圾回收机制
看了很多java内存管理的文章或者博客,写的要么笼统,要么划分的不正确,且很多文章都千篇一律.例如部分地方将jvm笼统的分为堆.栈.程序计数器,这么分太过于笼统,无法清晰的阐述java的内存管理模型: ...
- [C#学习笔记1]用csc.exe和记事本写一个C#应用程序
csc.exe是C#的命令行编译器(CSharpCompiler),可以编译C#源程序成可执行程序.它与Visual Studio等IDE(Integrated Development Environ ...
- MFC俄罗斯方块
经典俄罗斯方块游戏 源码百度云链接 链接:https://pan.baidu.com/s/14frk2EuFoiRCzudol2Xgvg提取码:syzk GitHub https://github.c ...
- c/c++ 多线程 ubuntu18.04 boost编译与运行的坑
多线程 boost编译与运行的坑 背景:因为要使用boost里的多线程库,所以遇到了下面的坑. 系统版本:ubuntu18.04 一,安装boost 1,去boost官网下载 boost_1_XX_0 ...
- logback日志框架的简单使用
1.首先在maven中增加依赖 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>l ...
- 「Python」为什么Python里面,整除的结果会是小数?
2018-06-08 参考资料:Python学习笔记(4)负数除法和取模运算 先来看三个式子(!这是在Python3.0下的运算结果): 输出结果: ‘//’明明是整除,为什么结果不是整数,而会出现小 ...
- ASP.NET Core 2.2 十九. Action参数的映射与模型绑定
前文说道了Action的激活,这里有个关键的操作就是Action参数的映射与模型绑定,这里即涉及到简单的string.int等类型,也包含Json等复杂类型,本文详细分享一下这一过程.(ASP.NET ...
- dump文件解析之探索.Net的内存
前言: 对于需要长时间运行的.net程序,有时需要我们查看内存的使用有没有内存泄露问题. 我们可以从dump文件中找到答案. Dump的看点 用dump文件来分析内存,到底我们需要关心哪些点呢? 内存 ...
- 【故障公告】10:30-10:45 左右 docker swarm 集群节点问题引发故障
非常抱歉,今天 10:30-10:45 左右由于 docker swarm 集群节点出现问题,造成除博客之外的站点出现访问异常,由此给您带来很大的麻烦,请您谅解. 故障开始时出现有时访问正常有时访问出 ...