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 ...
随机推荐
- Python requests快速上手
Python requests快速上手 这里参考官方文档,在ide中写了一遍,加深一下印象,定义的函数只是为了方便区分不同的请求方式 #-*-coding:utf-8-*- # Time:2017/1 ...
- Lua面向对象 --- 单例
GameManager.lua: --单例模式是利用一个全局表来实现的 GameManager = {} Manager = {__index = GameManager} function Game ...
- 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)
五.Graphics layer 1.新增Graphics layer Graphics layer用于显示用户自定义绘制的点.线.面图形.使用时确保xaml文件中Graphics layer定义 ...
- 12月8日 周五 image_tag.
Overview of helpers provided by Action View 6.1 AssetTagHelper:用于generate html语言 image_tag ,return a ...
- 页面title改变浏览器兼容性问题
前一阵子客户在界面上改了下小小的需求,需要点不同的文章title显示不同的模块名称(之前没有区分,统一叫新闻图片),很简单的一个需求但是测试的时候并没有注意到不兼容IE7和IE8.在客户那被尴尬的发现 ...
- Mysql查询用逗号分隔的字段-字符串函数FIND_IN_SET(),以及此函数与in()函数的区别
查询用逗号分隔的字段,可以用字符串函数FIND_IN_SET(): 查询数据库表中某个字段(值分行显示),可以用函数in(). 今天工作中遇到一个问题,就是用FIND_IN_SET()函数解决的. 第 ...
- Markdown语法笔记
1.文字和图片中怎么让图片换行? 答:在文字和图片之间加入多个空格或者直接按Tab健即可
- zk客户端的ClientCnxn类
ClientCnxn是客户端的类:该类管理zk客户端的socket io,维持一个可用服务器的列表. //ClientCnxn类 private final LinkedList<Packet& ...
- CSS3动画和JS动画的比较
前言 之前有被问到一个问题,css3动画和js动画性能谁更好,为什么.据我的经验,当然觉得css3动画性能更好,至于为什么一时还真答不上来,所以特意查了一下资料总结一波. JS动画 优点: js动画控 ...
- plsql导入excel文件
plsql导入excel文件 CREATE TABLE DWSB_GRMX1 ( XH VARCHAR2(40), SFZH VARCHAR2(40 ...