POJ 3494 Largest Submatrix of All 1's 栈的运用 好题
2015-05-30 10:55
381 查看
Language: Default 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 Source POJ Founder Monthly Contest – 2008.01.31, xfxyjwf |
1.都是由1组成
2.矩阵的面积最大
这道题,其实就是POJ2559的加强版
a[i][j]表示:第i行,以第j列为底的矩阵的最大高度。
之后,枚举每一列,相当于以每一列为底的条形图,求最大的矩形面积(就是POJ2559了)
注意:我这份代码交了3次,2次ac,1次tle,因为有些单样例tle了,下次再修改一下。
#include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; const int maxn=2005; int a[maxn][maxn]; int L[maxn]; int R[maxn]; struct Node { int num,h; }node[maxn]; int solve(int n,int cnt) { for(int i=1;i<=n;i++) { node[i].h=a[i][cnt]; node[i].num=i; } node[0].num=0; node[0].h=0; n++; node .num=n; node .h=0; stack<Node>s; s.push(node[0]); for(int i=1;i<=n;i++) { if(node[i].h>=s.top().h) s.push(node[i]); else { while(s.top().h>node[i].h) { Node tmp=s.top(); s.pop(); R[tmp.num]=i; L[tmp.num]=s.top().num; } s.push(node[i]); } } int ans=-1; for(int i=1;i<n;i++) { ans=max(ans,node[i].h*(R[i]-L[i]-1)); } return ans; } int main() { int row,col; while(~scanf("%d%d",&row,&col)) { for(int i=1;i<=row;i++) { a[i][0]=0; for(int j=1;j<=col;j++) { scanf("%d",&a[i][j]); if(a[i][j-1]&&a[i][j]) { a[i][j]+=a[i][j-1]; } } } int ans=-1; for(int j=1;j<=col;j++) { ans=max(ans,solve(row,j)); } printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- 社説 20150530 口永良部島噴火 円滑避難に生きた事前の備え
- 【swift】15-0530 闭包
- IIS日志分析的作用
- MySQL的复制架构与优化
- IIS7和Tomcat7整合,即IIS7和Tomcat共用80端口
- 强大的视差响应动画滑动图片切换效果
- 日经社説 20150530 安保は感情論でなく理詰めで論議せよ
- arduino的介绍
- css设计指南中有意思的部分
- android -- 蓝牙 bluetooth (五)接电话与听音乐
- 舍罕王的失算:不可低估等比数列和
- c++类模板和函数模板
- change user to root
- android -- 蓝牙 bluetooth (四)OPP文件传输
- GTD时间管理(1)---捕获搜集
- MySQL中无GROUP BY情况下直接使用HAVING语句的问题探究
- 基于MySQL认证的邮件系统
- 【套题】Bestcoder#42
- 日经春秋 20150530
- cc1: error: unrecognized command line option “-fforce-mem”