Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 9512   Accepted: 3406
Case Time Limit: 2000MS

Description

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

Source


 
思路:
这道题是二维数组,跟上一道题多了一维,那么我们就一行遍历,求一行中最大的矩形,再行遍历的时候就更新最大矩形面积。
 
 

 
代码:(数组)
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn = 2e3+;
int a[maxn][maxn];
int L[maxn],R[maxn];
int st[maxn];
int main(){
int m,n;
while(scanf("%d%d",&m,&n)!=EOF){
for(int i=;i<=n;i++)
a[][i] = ;
for(int i=;i<=m;i++){
for(int j=;j<n;j++){
scanf("%d",&a[i][j]);
if(a[i][j]!=)
a[i][j] = a[i][j] +a[i-][j]; //这里是关键
}
} int res = ;
for(int i=;i<=m;i++){
memset(st,,sizeof(st));
int t = ;
for(int j=;j<n;j++){
while(t>&&a[i][st[t-]]>=a[i][j]) t--;
L[j] = t==?:(st[t-]+);
st[t++] = j;
}
t=;
for(int j = n-;j>=;j--){
while(t>&&a[i][st[t-]]>=a[i][j]) t--;
R[j] = t==?n:(st[t-]);
st[t++] = j;
}
for(int j=;j<n;j++){
res=max(res,a[i][j]*(R[j]-L[j]));
}
}
cout<<res<<endl;
}
return ;
}
 代码2:栈
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 2e3+;
int a[maxn][maxn];
stack<int> s;
int main(){
int m,n;
while(cin>>m&&cin>>n){
for(int i=;i<=m;i++)
a[][i] = ;
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
scanf("%d",&a[i][j]);
if(a[i][j]!=)
a[i][j] = a[i-][j]+;
}
}
ll res = ;
for(int i=;i<=m;i++){
while(!s.empty()) s.pop();
int j = ;
while(j<=n+){
if(s.empty()||a[i][s.top()]<=a[i][j])
s.push(j++);
else{
int t=s.top();
s.pop();
ll wid = s.empty()?(j-):(j-s.top()-);
res = max(res,wid*a[i][t]);
}
}
}
cout<<res<<endl;
}
return ;
}
 
 

第一周任务Largest Submatrix of All 1’s的更多相关文章

  1. POJ-3494 Largest Submatrix of All 1’s (单调栈)

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8551   Ac ...

  2. 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 ...

  3. 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 ...

  4. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  5. Largest Submatrix 3

    Largest Submatrix 3 给出一个\(n\times m\)的网格图,第i行第j列上的格子有数字\(a[i][j]\),显然,你可以从中找到一个子矩阵,保证子矩阵中的数字互不相同,求子矩 ...

  6. Largest Submatrix

    Largest Submatrix 给出一个\(n\times m\)的网格,网格里只放有字符a,b,c,d,w,x,,z,现在你可以将其中的w换成a,b,把x换成b,c,把y换成a,c,把z换成a, ...

  7. 第一周 总结笔记 / 斯坦福-Machine Learning-Andrew Ng

    课程主页:https://www.coursera.org/learn/machine-learning/home/welcome 收集再多的资料也没用,关键是要自己理解总结,做笔记就是一个归纳总结的 ...

  8. Surprise团队第一周项目总结

    Surprise团队第一周项目总结 团队项目 基本内容 五子棋(Gobang)的开发与应用 利用Android Studio设计一款五子棋游戏,并丰富其内涵 预期目标 实现人人模式:2个用户可以在同一 ...

  9. 20145213《Java程序设计》第一周学习总结

    20145213<Java程序设计>第一周学习总结 教材学习内容总结 期待了一个寒假,终于见识到了神秘的娄老师和他的Java课.虽说算不上金风玉露一相逢,没有胜却人间无数也是情理之中,但娄 ...

随机推荐

  1. sql server 与 oracle的区别(转)

    --1.数据类型不同.      --sql server 的数据类型:int ,smallint ,char,varchar,nchar,nvarchar,ntext,datetime,smalld ...

  2. leetcode python翻转字符串里的单词

    # Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: ...

  3. jmeter 响应超时时间设置 压力增大,不能正常退出全部线程

    当压力增大会出现connect timeout error 压力增大,不能正常退出全部线程: 解决办法:http request default--advance--timeouts 如填写1,表示大 ...

  4. css篇-简化版

    [CSS篇]简化版 (1)     CSS盒模型 CSS盒模型 题目:谈谈你对CSS盒模型的认识 1)       基本概念:标准模型+IE模型 2)       标准模型和IE模型的区别 计算宽度和 ...

  5. TurtleBOT3

    ubuntu更换源 sudo cp /etc/apt/sources.list /etc/apt/sources_backup.list sudo gedit /etc/apt/sources.lis ...

  6. 出现异常: 非介入式客户端验证规则中的验证类型名称必须唯一。下列验证类型出现重复: required

    在将web.config文件中的<add key="ClientValidationEnabled" value="false" /> 设为fals ...

  7. android5.1 修改音量键绑定多媒体声音

    修改此文件frameworks/base/media/java/android/media/AudioService.java中函数 private int getActiveStreamType(i ...

  8. Linux中目录结构以及VI编辑器常见的命令操作

    1.每个目录的详细介绍,先放一张目录的整体结构在这里 /bin:是Binary的缩写,用于存放经常使用的命令 /sbin:s代表Super User,用于存放系统管理员使用的命令 /home:存放普通 ...

  9. java笔试手写算法面试题大全含答案

    1.统计一篇英文文章单词个数.public class WordCounting {public static void main(String[] args) {try(FileReader fr ...

  10. C++32位和64位常见类型的大小

             32位      64位 char      1       1 int                       4      大多数4,少数8 long      4      ...