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 ...
随机推荐
- Ajax常见面试题
1,什么是ajax? 为什么要使用ajax? 1.ajax是"asynchornous javascript and xml "的缩写,指一种创建交互式网页应用的网页开发技术. 2 ...
- cesium 之地图显示坐标、比例尺、海拔高度效果篇(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- Java递归方法遍历二叉树的代码
将内容过程中经常用的内容做个记录,如下内容内容是关于Java递归方法遍历二叉树的内容. package com.wzs; public class TestBinaryTree { public st ...
- 逻辑回归&线性支持向量机
代码: # -*- coding: utf-8 -*- """ Created on Tue Jul 17 10:13:20 2018 @author: zhen &qu ...
- ubuntu18.04 ssh 远程系统拒绝连接 解决方法
错误提示是这个: The remote system refused the connection. 原因是 Ubuntu 没安装 一个软件, 废话不多说 ,上解决方法: 执行该条命令,安装 ,安装 ...
- python之list和tuple
https://www.cnblogs.com/evablogs/p/6691743.html list和tuple区别: 相同:均为有序集合 异同:list可变,tuple一旦初始化则不可变 lis ...
- js用canvans 实现简单的粒子运动
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- js实现语音功能
在项目中需要对ajax请求返回的消息进行语音播报.那么什么录制的就是在太low啦.下面js贴代码 str 为返回的data //语音播报function voiceAnnouncements(str) ...
- 运行SSIS包的六种方式
注意: 1~5都需要SSIS安装在对应的机器上. 一.直接在Data Tool里运行 右键选择要运行的包,然后直接选择运行包 二.使用SQL Server的Job作业 可以选择:a. File Sys ...
- java基础之-I/O流和File类解析
在日常的java开发中少不了文件的读取和 写入,这就涉及到文件的I/O操作,今天就来总结下文件的IO操作,顺便文件的IO操作也需要File了的帮助,所以一起总结了. 以下图片为我根据其他博客所总结的内 ...