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 ...
随机推荐
- cookie session localstorage sessionStorage区别
cookie:http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html 重要特点: 1.cookie 有大小设置,有过期时间设 ...
- Apache配置文件httpd.conf细说
1.httpd.conf文件位于apache安装目录/conf下2.Listen 88表示监听端口88 此处可以连续写多个端口监听如下: Listen 88 Listen 809 3.目录配置如下: ...
- python怎样压缩和解压缩ZIP文件
https://zhidao.baidu.com/question/1498409764366387259.html
- mac 无法打开xx ,因为无法确认开发者身份
系统偏好与设置 - 安全性与隐私 - 通用 允许从以下位置下载的应用: 选择 [任何来源],如果没有这个选项,使用终端执行下面的命令: spctl --master-disable (spctl空格 ...
- java concurrent包的实现原理
由于java的CAS同时具有 volatile 读和volatile写的内存语义,因此Java线程之间的通信现在有了下面四种方式: A线程写volatile变量,随后B线程读这个volatil ...
- 广播 (Broadcast)
广播 :在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内 ...
- Kali Linux on Android # 实测:小米2s离线安装Kali Linux
小米2s 离线安装Kali Linux 2017年2月4日 by 小甘丶 前段时间也研究过一两天,没弄明白,今天突然来兴致了,说研究一下吧,结果一不小心,就弄明白了! 第一次研究,主要是没弄明白这个 ...
- jquery自动填充输入框
1,这是一个比较简单的页面,你可以复制下来就可以使用.<!doctype html><html lang="en"><head> <met ...
- kill word fore out
1● fore f ɔ: 预先,前面
- learning shell get script absolute path (3)
Shell get script absolute path [Purpose] Get shell script absolute path [Eevironment] ...