Largest Submatrix of All 1’s (单调队列)
来源:互联网 发布:java disruptor 教程 编辑:程序博客网 时间:2024/06/05 19:55
Largest Submatrix of All 1’s
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 41 Accepted Submission(s) : 21
Problem Description
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? Bylargest we mean that the submatrix has the most elements.
Input
<p>The input contains multiple test cases. Each test case begins with <i>m</i> and <i>n</i> (1 ≤ <i>m</i>, <i>n</i> ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on <i>m</i> lines each with <i>n</i> numbers. The input ends once EOF is met.</p>
Output
<p>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.</p>
Sample Input
2 20 00 04 40 0 0 00 1 1 00 1 1 00 0 0 0
Sample Output
04
Source
PKU
题意:
给你一个矩阵,由0和1组成,让你求最大的一块矩阵。
思路:
一开始毫无头绪,但是后来仔细一想,只需要将矩阵进行压缩即可,注意:这里的压缩不是整列压缩,而是一行一行压缩,这就转化成了前面所做过的
最大面积的了。
代码:
#include<iostream>#include<cstring>#include<stdio.h>using namespace std;int main(){int t[2010];int h[2010];int l[2010];int r[2010];int s[2010];int n,m;int maxx;while(~scanf("%d%d",&n,&m) && n && m){ maxx=0; memset(t,0,sizeof(t)); memset(h,0,sizeof(h)); memset(s,0,sizeof(s)); for(int k=1;k<=m;k++){ for(int j=1;j<=n;j++) { scanf("%d",&t[j]); if(t[j]==0)h[j]=0; else h[j]+=t[j]; l[j]=j;r[j]=j; }int top=0;for(int i=1;i<=n;i++){ while(top && h[i]<=h[s[top]])top--;if(top==0)l[i]=1;else l[i]=s[top]+1;s[++top]=i;}top=0;for(int i=n;i>=1;i--){ while(top && h[i]<=h[s[top]])top--; if(top==0) r[i]=n; else r[i]=s[top]-1; s[++top]=i; }int maxn=0;int summ;for(int i=1;i<=n;i++){summ=h[i]*(r[i]-l[i]+1);if(maxn<summ)maxn=summ;}if(maxx<maxn)maxx=maxn;}printf("%d\n",maxx);}return 0;}
那个dp求最大矩阵和压缩方法为:
for(i=1;i<=n;i++){ for(j=1;j<=n;j++)b[j]=0;//初始化 for(j=i;j<=n;j++){ for(k=1;k<=n;k++) b[k]+=a[j][k]; }.....}
阅读全文
1 0
- Largest Submatrix of All 1’s (单调队列)
- Largest Submatrix of All 1’s (单调队列)
- Largest Submatrix of All 1’s--(单调队列)
- POJ3494---Largest Submatrix of All 1’s(单调栈)
- poj3494 Largest Submatrix of All 1’s 单调栈+dp
- 单调栈 Largest Submatrix of All 1’s poj3494
- POJ3494 Largest Submatrix of All 1’s[单调栈]
- Largest Submatrix of All 1’s(POJ 3494) 单调栈
- Largest Submatrix of All 1’s----单调栈
- poj 3494 Largest Submatrix of All 1’s(单调栈)
- 单调栈--poj3494 Largest Submatrix of All 1’s
- POJ 3494 Largest Submatrix of All 1’s(单调栈||dp)
- POJ-3494 Largest Submatrix of All 1’s(单调栈)
- 2017省选拔(二)poj3494 Largest Submatrix of All 1’s (单调栈+预处理)
- poj 3494 Largest Submatrix of All 1’s(单调栈)
- poj3494 Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s--POJ3494
- Largest Submatrix of All 1’s POJ
- Day30
- 关于辗转相除法
- js关于document和window对象
- Android动画之View animation(视图动画)
- 单链表解析Ⅱ
- Largest Submatrix of All 1’s (单调队列)
- BZOJ 1911 [APIO2010]特别行动队
- 【UE4小白奋斗记录】BluePrint+样条曲线的路径动画(可循环)
- uva 11491 Erasing and Winning 奖品的价值(贪心)
- 【 UVa 10139】 Factovisors 【 m | n!? 】
- 深度学习知识点(零散)
- Unity3d搭建与调用webservice方法
- 2017.08.14【NOIP 普及组】模拟赛C组总结
- 数据库学习纪要(三):hive-3