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 ...
随机推荐
- Maven常用dependency记录
1.servlet配置 <dependency> <groupId>junit</groupId> <artifactId>junit</arti ...
- c# 获取方法所在的命名空间 类名 方法名
平时我们在记录日志的时候难免会需要直接记录当前方法的路径,以便查找,但是每次都输入方法名称非常的繁琐,同时如果修改了方法名称也要去手动修改日志内容,真的是劳命伤财啊,所以有了如下方法则可解决我们的大难 ...
- English trip -- VC(情景课)8 D Reading
Listen and read. Shop Smart [smɑːt] Employee of the Month: Sara['særə] (萨拉) Lopez(洛佩斯) Congratulati ...
- gradle 编译 No such property: sonatypeUsername错误解决
No such property: sonatypeUsername for class: org.gradle.api.publication.maven.internal.ant.DefaultG ...
- python-day21--time模块
一.三种表示方法 1.时间戳(timestamp): time.time( ) #得到的是float类型 2.格式化(Format String): time.strftime('%Y/% ...
- OC 对象和函数
#import <Foundation/Foundation.h> @interface Car : NSObject {// 成员变量 @public int wheels; int s ...
- [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- hadoop mysql install (5)
reference : http://dblab.xmu.edu.cn/blog/install-mysql/ http://wiki.ubuntu.org.cn/MySQL #install mys ...
- snapshot相关
概述 Specify the number of days of snapshots to choose from Entering the number of days (n) will resul ...
- Maven 打jar包,pom文件配置
以下是pom.xml文件的相关配置. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...