第一周任务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课.虽说算不上金风玉露一相逢,没有胜却人间无数也是情理之中,但娄 ...
随机推荐
- 慎用margin系列3---IE6下3px bug
当我们想让一段文字出现在一个布局块的右边的时候,我们会让布局块向左浮动,然后把段落设置左布局块宽度大小的左空白.比如左布局块是.a{float:left;height: 110px;width: 28 ...
- 跨域篇--JSONP原理
一篇文章让你明白 jsonp原理详解 什么是JSONP? 先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,着用自 ...
- The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间
/*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...
- Java 简单链表实现
1.初衷 最近在看Java基础,有一章节涉及到链表,便记之所学所得. 2.链表的定义 链表是存储地址不连续的线性存储结构 3.链表的基本结构 一个节点包含当前节点,与下个节点的引用 4.简单链表的实现 ...
- 使mysql数据库支持简体中文
永久支持简体中文[root@localhost ~]# vim /etc/my.cnf添加如下四行:[client]default-character-set=utf8 [mysql]default- ...
- 杭电多校第四场-H- K-th Closest Distance
题目描述 You have an array: a1, a2, , an and you must answer for some queries.For each query, you are g ...
- Codeforces 343E 最小割树
题意及思路:https://www.cnblogs.com/Yuzao/p/8494024.html 最小割树的实现参考了这篇博客:https://www.cnblogs.com/coder-Uran ...
- HTTP、HTTPS等常用服务的默认端口号
口号标识了一个主机上进行通信的不同的应用程序. 1.HTTP协议代理服务器常用端口号:80/8080/3128/8081/9098 2.SOCKS代理协议服务器常用端口号:1080 3.FTP(文件传 ...
- JavaWeb开发之一《Tomcat服务器的部署、安装及应用》
搬以前写的博客[2014-12-10 21:43] 这几天做了一个Java的程序,然后先把他搭载到Web上,于是学习了基于Tomcat服务器的web开发,这里回顾一下Tomcat服务器的搭建过程. 1 ...
- mac MAMP安装redis扩展
一般情况下目录大概是一样的,只是php的版本不同,所以选择好自己对应的php版本目录即可 git clone https://github.com/nicolasff/phpredis.git cd ...