POJ3494Largest 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 ≤ 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
最大全1子矩阵
对于每一行,维护一个全1高度(tot)递减栈就行了
//
// main.cpp
// poj3494
//
// Created by Candy on 10/5/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=2e3+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,tot[N],a,ans=;
struct data{
int h,l,pos;
}st[N];
int top=;
int main(int argc, const char * argv[]) {
while(scanf("%d%d",&n,&m)!=EOF){
ans=;
memset(tot,,sizeof(tot));
for(int i=;i<=n;i++){
top=;
for(int j=;j<=m;j++){
a=read();
if(!a) tot[j]=-;
data t;
t.h=++tot[j];t.l=;t.pos=j;
int right=;
while(top&&st[top].h>=t.h){
ans=max(ans,(st[top].l+right)*st[top].h);
//printf("%d %d %d %d %d %d\n",i,j,ans,st[top].l,right,st[top].h);
right+=st[top].l; t.l+=st[top].l;
top--;
}
st[++top]=t;//printf("top %d\n",top);
}//printf("toptop %d\n",top);
while(top){
ans=max(ans,st[top].h*(st[top].l +n-st[top].pos));
//printf("p %d %d %d %d\n",st[top].h,st[top].pos,st[top].l,ans);
top--;
}
}
printf("%d\n",ans);
} return ;
}
POJ3494Largest Submatrix of All 1’s[单调栈]的更多相关文章
- 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 ...
- 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 ...
- HDU 2870 Largest Submatrix (单调栈)
http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...
- [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
- hdu2870 Largest Submatrix 单调栈
描述 就不需要描述了... 题目传送门 题解 被lyd的书的标签给骗了(居然写了单调队列优化dp??) 看了半天没看出来哪里是单调队列dp,表示强烈谴责QAQ w x y z 可以各自 变成a , ...
- SPOJ MINSUB - Largest Submatrix(二分+单调栈)
http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ...
- spoj MINSUB 单调栈+二分
题目链接:点击传送 MINSUB - Largest Submatrix no tags You are given an matrix M (consisting of nonnegative i ...
随机推荐
- [deviceone开发]-HeaderView和FooterView的示例
一.简介 这个是利用do_ListView组件实现下拉和上拉刷新功能的例子,除了do_ListView,其它比如do_Webview,do_ScrollView都有这个个功能.对应的BBS里的帖子详细 ...
- DOM事件类型详解
一.表单事件: input事件当<input>.<textarea>的值发生变化时触发.此外,打开contenteditable属性的元素,只要值发生变化,也会触发input事 ...
- Telerik JustDecompile 2014.1.255.0 开发版(.NET反编译神器,免费下载)
Telerik JustDecompile是Telerik公司推出一个免费的.NET反编译工具,支持插件与Visual Studio 2015~2013集成,还能够创建Visual Studio Pr ...
- AdaBoost算法实现
# -*- coding: utf-8 -*- # -------------------------------------------------------------------------- ...
- 【IOS】ios中NSUserDefault与android中的SharedPreference用法简单对比
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3405308.html 有Android开发经验的朋友对Shar ...
- iOS自动布局进阶用法
本文主要介绍几个我遇到并总结的相对高级的用法(当然啦牛人会觉得这也不算什么). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以 ...
- 图解六大UML类图关系
在学习UML类图的过程中,UML类图关系是必须要掌握的问题,UML定义的关系主要有六种:依赖.类属.关联.实现.聚合和组合.下面对其定义和表示方法逐一说明. UML类图关系简介 依赖(Dependen ...
- iOS通讯录开发
场景一:直接选择一个联系人的电话号码 这里不需要先获取所有的联系人自己做联系人列表,直接使用系统自带的AddressBookUI/ABPeoplePickerNavigationController. ...
- iOS 远程推送通知
1.什么是推送通知 在某些特殊情况下,应用程序被动收到的以不同种界面形式出现的提醒信息 推送通知的作用:可以让不在前台运行的app通知app发生了改变 iOS中得推送通知种类 远程推送通知(Remot ...
- YUM源的简介,配置与使用
A.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由yellow dog 这一发行版的 ...