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 ...
随机推荐
- MySQL事务及ACID特性
一.事物 1.定义:事务是访问和更新数据库的程序执行单元,事务中包含一条或者多条SQL语句,这些语句要么全部执行成功,要么都不执行. 在MySQL中,事务支持是在引擎层实现的,MySQL是一个支持多引 ...
- js 滚轮控制图片缩放大小和拖动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- fab 菜单实现之前传-钟表表盘
个人很喜欢谷歌的material design,很喜欢但是没有动手弄过,今天想动手操作一下Floating Action Button菜单,网上有很多种:圆形.扇形.射线.直线等.我想在一个例子中用到 ...
- Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码
Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...
- 【English】三、以o结尾单词变复数
一.以O结尾的词,许多加es构成复数,特别是一些常用词如: potatoes 土豆 tomatoes 西红柿 echoes 回声 tornadoes 龙卷风 torpedoes ...
- SQL SERVER 2012 AlwaysOn– 数据库层面 02
搭建 AlwaysOn 是件非常繁琐的工作,需要从两方面考虑,操作系统层面和数据库层面,AlwaysOn 非常依赖于操作系统,域控,群集,节点等概念: DBA 不但要熟悉数据库也要熟悉操作系统的一些概 ...
- VirtualBox Network Config
Sharing Host VPN with VirtualBox guest After looking for this solution everywhere, I finally found a ...
- 我的第一个python web开发框架(38)——管理员管理功能
后台管理员的管理功能,它主要用来管理后台的登录账号,绑定权限,当然如果想将后台管理扩展成企业相关管理系统,比如用于公司人事管理,在这个基础上进行适当扩展就可以了. 我们先看看界面效果(也可以看着数据字 ...
- express+sequelize 做后台
第一部分:安装express 第一步:执行 npm install -g express-generator note:必须安装这个,不然创建express项目的时候会提示express命令没有找到 ...
- Net包管理NuGet(2)nuget包的生成方法
1,下载NuGetPackageExplorer,可以下载运行源码,也可以直接下载安装包安装安装之后打开 设置好内容之后点击绿色的打钩保存然后操作右边空白处 然后点击File>Save;保存之后 ...