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 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 mlines 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 很巧妙的方法。把图拆成一行行来做,分别求以每行为底的最大矩形面积,类似lc课后辅导。最后比较出最大值即可。
#include<stdio.h>
#include<stack>
using namespace std; int a[][];
int l[],r[]; int main()
{
int n,m,max,i,j;
while(~scanf("%d%d",&n,&m)){
max=;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
scanf("%d",&a[i][j]);
if(a[i][j]==) a[i][j]+=a[i-][j];
}
}
for(i=;i<=n;i++){
stack<int> s;
for(j=;j<=m;j++){
while(s.size()&&a[i][s.top()]>=a[i][j]) s.pop();
l[j]=s.size()==?:s.top()+;
s.push(j);
}
while(s.size()){
s.pop();
}
for(j=m;j>=;j--){
while(s.size()&&a[i][s.top()]>=a[i][j]) s.pop();
r[j]=s.size()==?m:s.top()-;
s.push(j);
}
for(j=;j<=m;j++){
if(a[i][j]*(r[j]-l[j]+)>max) max=a[i][j]*(r[j]-l[j]+);
}
}
printf("%d\n",max);
}
return ;
}
POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵的更多相关文章
- 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 3494 Largest Submatrix of All 1’s(最大全1子矩阵)
题目链接:http://poj.org/problem?id=3494 题意:给出一个01的矩阵,找出一个面积最大的全1矩阵. 思路:用h[i][j]表示从位置(i,j)向上连续1的最大长度.之后枚举 ...
- 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
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 ...
随机推荐
- RabbitMQ的工作模式
简单模式: # #########################基于简单模式的 生产者 ######################### #!/usr/bin/env python import ...
- Java爬虫快速开发工具uncs的部署攻略
写在前面 uncs是java快速开发爬虫的工具,简单便捷,经过大量版本迭代和生产验证,可以适用大多数网站,推荐使用. 一.基本用法 1.1 开发包获取 目前只能在公司内网maven服务器获取到 < ...
- C# Winform 中webBrowser显示html内容时禁止错误提示的方法
在winform中有一个控件可以显示html的内容,该控件就是webbrowser,设置它的DocumenText属性为HTML的内容即可. 在使用WebBrowser做UI的时候,我们有时不希望里面 ...
- 九度OJ 1121:首字母大写 (字符串处理)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2865 解决:1007 题目描述: 对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母. 在字符串中,单词 ...
- 九度OJ 1120:全排列 (DFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4471 解决:1139 题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < ...
- cpio
1 压缩 -o,生成cpio格式的归档文件.从标准输入获取文件名列表. 2 解压 -i,对cpio格式的归档文件进行解压,生成单个的文件. 3 --null 从标准输入获取的文件名列表为"\ ...
- RNN 的入门程序DEMO
1.视频介绍 https://www.youtube.com/watch?v=cdLUzrjnlr4 2. https://github.com/llSourcell/recurrent_neural ...
- Ace(一)环境搭建
1.下载ACE源码代码 http://www.cs.wustl.edu/~schmidt/ACE.html 2.编译源代码 2.1 进入源码包解压后的ACE_wrappers\ace目录, ...
- 深入Vue.js从源码开始(二)
从入口开始 我们之前提到过 Vue.js 构建过程,在 web 应用下,我们来分析 Runtime + Compiler 构建出来的 Vue.js,它的入口是 src/platforms/web/en ...
- 矩阵管理——和visitor模式没有本质区别,都是为了避免资源重复
矩阵管理中的员工是双线汇报的模式.其上司有两个,一个是流程上司,一个是专业上司.流程上司负责你的日常考核,专业上司负责你的晋升和任免. 管理条件 相对于矩阵管理的矩阵式组织,适合于某些较为庞大的全球性 ...