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 m lines 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 单调栈,每一行记录个列的高度 然后每一行进行单调栈,用输入输出流还是超时得用scanf。。或者 用scanf就不错。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
int n,m,d;
int mp[][];
int l[]={},r[],stack[],top,maxi,sum;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0); while(scanf("%d%d",&n,&m)!=EOF)
{
maxi=;
for(int i=;i<=n;i++)
{
top=;
for(int j=;j<=m;j++)
{
scanf("%d",&d);
//mp[i][j]=mp[i][j-1]+d; 不对的 中间如果有0 应该中断
mp[i][j]=(d==?:(mp[i-][j]+d));
l[j]=r[j]=j;
}
for(int j=;j<=m+;j++)
{
while(top>&&mp[i][j]<=mp[i][stack[top-]])
{
r[stack[top-]]=j-;
sum=(r[stack[top-]]-l[stack[top-]]+)*mp[i][stack[top-]];
if(sum>maxi)maxi=sum;
top--;
}
l[j]=stack[top-]+;
stack[top++]=j;
}
} printf("%d\n",maxi);
}
}
/*
4 4
1 0 1 1
0 1 1 1
0 0 1 1
0 1 1 1
*/
Largest Submatrix of All 1’s的更多相关文章
- Largest Submatrix(动态规划)
Largest Submatrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)
Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change ...
- codeforces 407D Largest Submatrix 3
codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ...
- 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 m ...
- 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 单调栈求最大子矩阵
Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...
- HDU 2870 Largest Submatrix (单调栈)
http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...
- MINSUB - Largest Submatrix
MINSUB - Largest Submatrix no tags You are given an matrix M (consisting of nonnegative integers) a ...
随机推荐
- ninja install error
ninja install ...... CMake Error at cmake_install.cmake:36 (FILE): file INSTALL cannot find " ...
- CSS——图片替换方法:Fahrner图片替换法(FIR)
Html: <h1 id="fir"><span>Fahrner Image Replacement</span></h1> CSS ...
- 同步代码时忽略maven项目 target目录
方式一: 在项目代码路径,如: F:\xyx\sl 鼠标右键,“TortoiseSVN”-- >“Settings” -->"Subversion"-->&qu ...
- Linq 使用skip和take分页
static int Main(string[] args) { //每页条数 const int pageSize = 2; //页码 0就是第一条数据 int pageNum = 0; strin ...
- 16 Managing Undo
16 Managing Undo 从Oracle11g开始,在默认安装中oracle会自动管理undo, 典型安装中不需要DBA介入配置,然而,如果选择了flash back特性,你就需要进行一些un ...
- XSD与C#Code以及XML之间的相互关心
------------------------------网上参考资料 C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件:http://blog.sina.com.cn/s/b ...
- Python下图片的高斯模糊化的优化
资源下载 #本文PDF版下载 Python下图片的高斯模糊化的优化(或者单击我博客园右上角的github小标,找到lab102的W6目录下即可) #本文代码下载 高斯模糊(一维)优化代码(和本文方法集 ...
- Java使用FFmpeg处理视频文件指南
Java使用FFmpeg处理视频文件指南 本文主要讲述如何使用Java + FFmpeg实现对视频文件的信息提取.码率压缩.分辨率转换等功能: 之前在网上浏览了一大圈Java使用FFmpeg处理音视频 ...
- python-day34--并发编程之多线程
理论部分 一.什么是线程: 1.线程:一条流水线的工作过程 2.一个进程里至少有一个线程,这个线程叫主线程 进程里真正干活的就是线程 3.进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资 ...
- python-day20--collections模块
1.namedtuple: 生成可以使用名字来访问元素内容的tuple >>> from collections import namedtuple >>> Poi ...