Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 5883   Accepted: 2217
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 ≤ mn ≤ 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

  1. 2 2
  2. 0 0
  3. 0 0
  4. 4 4
  5. 0 0 0 0
  6. 0 1 1 0
  7. 0 1 1 0
  8. 0 0 0 0

Sample Output

  1. 0
  2. 4

Source


最大全1子矩阵

对于每一行,维护一个全1高度(tot)递减栈就行了
注意0时tot[j]=-1,相当于一个高度为0的
最后依旧要弹出栈里的,如果没有上一行连sample都错
注意l已经包含了这个点,最后+n-st[top].pos不需要再+1
  1. //
  2. // main.cpp
  3. // poj3494
  4. //
  5. // Created by Candy on 10/5/16.
  6. // Copyright © 2016 Candy. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <algorithm>
  12. #include <cstring>
  13. using namespace std;
  14. typedef long long ll;
  15. const int N=2e3+;
  16. inline int read(){
  17. char c=getchar();int x=,f=;
  18. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  19. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  20. return x;
  21. }
  22. int n,m,tot[N],a,ans=;
  23. struct data{
  24. int h,l,pos;
  25. }st[N];
  26. int top=;
  27. int main(int argc, const char * argv[]) {
  28. while(scanf("%d%d",&n,&m)!=EOF){
  29. ans=;
  30. memset(tot,,sizeof(tot));
  31. for(int i=;i<=n;i++){
  32. top=;
  33. for(int j=;j<=m;j++){
  34. a=read();
  35. if(!a) tot[j]=-;
  36. data t;
  37. t.h=++tot[j];t.l=;t.pos=j;
  38. int right=;
  39. while(top&&st[top].h>=t.h){
  40. ans=max(ans,(st[top].l+right)*st[top].h);
  41. //printf("%d %d %d %d %d %d\n",i,j,ans,st[top].l,right,st[top].h);
  42. right+=st[top].l; t.l+=st[top].l;
  43. top--;
  44. }
  45. st[++top]=t;//printf("top %d\n",top);
  46. }//printf("toptop %d\n",top);
  47. while(top){
  48. ans=max(ans,st[top].h*(st[top].l +n-st[top].pos));
  49. //printf("p %d %d %d %d\n",st[top].h,st[top].pos,st[top].l,ans);
  50. top--;
  51. }
  52. }
  53. printf("%d\n",ans);
  54. }
  55.  
  56. return ;
  57. }
 

POJ3494Largest Submatrix of All 1’s[单调栈]的更多相关文章

  1. POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵

    Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...

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

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

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

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

  5. HDU 2870 Largest Submatrix (单调栈)

    http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...

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

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

  7. hdu2870 Largest Submatrix 单调栈

    描述 就不需要描述了... 题目传送门 题解 被lyd的书的标签给骗了(居然写了单调队列优化dp??)  看了半天没看出来哪里是单调队列dp,表示强烈谴责QAQ w x y z  可以各自 变成a , ...

  8. SPOJ MINSUB - Largest Submatrix(二分+单调栈)

    http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ...

  9. spoj MINSUB 单调栈+二分

    题目链接:点击传送 MINSUB - Largest Submatrix no tags  You are given an matrix M (consisting of nonnegative i ...

随机推荐

  1. iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html

    错误日志: Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacc ...

  2. SharePoint2013的头像显示和读取

      前言 有个时候SP的二次开发,需要用代码获取头像显示,如SP基于AD验证,AD有头像属性,做为头像数据来源(因为Exchange和lync的头像也来自AD),说道这里大家都知道有2种办法,一种从A ...

  3. spring+ibatis+多数据源

    环境:spring3.1+ibatis2.3.4+oracle+hbase要求:需要在工程中操作两个不同的数据源,一个是mssql,另一个是hbase.实现:    <bean id=" ...

  4. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q16-Q18)

    Question 16 You are designing a SharePoint 2010 solution to manage statements of work. You need to d ...

  5. Java File类总结和FileUtils类

    Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...

  6. XMPP框架的分析、导入及问题解决

    上一篇讲了 XMPP调试与简单使用 ,本篇开始讲如何使用将XMPPFramework框架导入到项目中! 先来了解以下XMPPFramework都放了些什么: Authentication: 与登陆相关 ...

  7. 你真的了解UIViewController生命周期吗?

    一:首先了解一下生命周期图 二:UIViewController 生命周期介绍 1.通过alloc init 分配内存,初始化controller. 2.loadView loadView方法默认实现 ...

  8. Android的生命周期学习

    掌握Android的生命周期对于如何一个刚刚接触Android初学者来说是至关重要的,在然后的开发中会给我留有更多的时刻余地,当自己正在认识Android中整个声明周期后,会编写出更加的流畅的程序 应 ...

  9. 浅谈Java五大设计原则之责任链模式

    首先我们得先定义一个责任链模式: 责任链模式是一种线性执行流程,多个对象都有机会去执行同一个任务,只是在执行过程中, 由于执行的权利和范围不一样,那么当自己不能处理此任务时,就必须将这个任务抛给下一个 ...

  10. iOS---TextView显示HTML文本

    _checkAllIntroduceTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth-20, kS ...