第一周任务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 ;
}
#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的更多相关文章
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- 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 ...
- [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
- Largest Submatrix 3
Largest Submatrix 3 给出一个\(n\times m\)的网格图,第i行第j列上的格子有数字\(a[i][j]\),显然,你可以从中找到一个子矩阵,保证子矩阵中的数字互不相同,求子矩 ...
- 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, ...
- 第一周 总结笔记 / 斯坦福-Machine Learning-Andrew Ng
课程主页:https://www.coursera.org/learn/machine-learning/home/welcome 收集再多的资料也没用,关键是要自己理解总结,做笔记就是一个归纳总结的 ...
- Surprise团队第一周项目总结
Surprise团队第一周项目总结 团队项目 基本内容 五子棋(Gobang)的开发与应用 利用Android Studio设计一款五子棋游戏,并丰富其内涵 预期目标 实现人人模式:2个用户可以在同一 ...
- 20145213《Java程序设计》第一周学习总结
20145213<Java程序设计>第一周学习总结 教材学习内容总结 期待了一个寒假,终于见识到了神秘的娄老师和他的Java课.虽说算不上金风玉露一相逢,没有胜却人间无数也是情理之中,但娄 ...
随机推荐
- leetcode python翻转字符串里的单词
# Leetcode 151 翻转字符串里的单词### 题目描述给定一个字符串,逐个翻转字符串中的每个单词. **示例1:** 输入: "the sky is blue" 输出: ...
- Java 13 即将发布,新特性必须抢先看!
作者:h4cd 本文转载自开源中国(ID:oschina2013) 由于 Java 现在采取"半年发布一次新版本"的模式,所以 Java 12 的下一个版本 Java 13/JDK ...
- 8种常见SQL错误用法,你中招了吗?
作者:db匠 来源:https://yq.aliyun.com/articles/72501 1.LIMIT 语句 分页查询是最常用的场景之一,但也通常也是最容易出问题的地方.比如对于下面简单的语句, ...
- 9、springcloud整合logback打印sql语句
Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core.logback- classic和logback-access.logback-c ...
- JS综合面试题1
function foo(){ getName = function () { alert(1); }; return this; } Foo.getName = function(){ alert( ...
- Python 基础 4-1 字典入门
引言 字典 是Python 内置的一种数据结构,它便于语义化表达一些结构数据,字典是开发中常用的一种数据结构 字典介绍 字典使用花括号 {} 或 dict 来创建,字典是可以嵌套使用的 字典是成对出现 ...
- Linux的一些命令总结
启动终端: ctr+alt+t 终端字体放大: ctr+shift+'+',终端字体缩小: ctr+'-' ls: 查看当前目录下的文件信息 pwd: 查看目录所在的路径 touch: 创建文件 mk ...
- linux挂载群辉的NFS共享文件夹
mount -t nfs 192.168.137.136:/volume1/NFSfile /NFSfile -o proto=tcp -o nolock df -h #查看挂载点
- spring 新建mybatis ...
一. 创建bean类 package com.feilong.blog.dao; public class Message { private int id; private String autho ...
- vue 表单输入绑定 checkbox
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...