类型:单调栈

传送门:>Here<

题意:求一个$01$矩阵中最大子矩形(全是$1$)的面积

解题思路

单调栈的一个经典应用

考虑维护一个数组$p[i][j]$表示$(i,j)$往上最多有多少个连续的$1$。于是问题就转化为上一题的问题了,$p$即为高度,往左右扩散,利用单调栈求即可。总复杂度$O(n^2)$

个人认为,会做这类题的关键还是彻底弄懂上一题

Code

/*By DennyQi 2018.8.18*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int MAXM = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x<<) + (x<<) + c - '', c = getchar();return x * w;
}
int N,M,top,ans,cnt; char c[];
int a[][],p[][],h[],w[];
int main(){
scanf("%d %d",&N,&M);
getchar();
for(int i = ; i <= N; ++i){
for(int j = ; j <= M; ++j){
scanf("%s",c);
a[i][j] = (c[] == 'F' ? : );
}
}
for(int i = ; i <= N; ++i){
for(int j = ; j <= M; ++j){
if(a[i][j]){
p[i][j] = p[i-][j] + ;
}
else{
p[i][j] = ;
}
}
}
for(int i = ; i <= N; ++i){
top = ;
for(int j = ; j <= M; ++j){
if(!top || p[i][j] > h[top]){
h[++top] = p[i][j];
w[top] = ;
}
else{
cnt = ;
while(top > && p[i][j] <= h[top]){
cnt += w[top];
ans = Max(ans, h[top] * cnt);
--top;
}
h[++top] = p[i][j];
w[top] = cnt+;
}
}
cnt = ;
while(top > ){
cnt += w[top];
ans = Max(ans, h[top] * cnt);
--top;
}
}
printf("%d", ans * );
return ;
}

[洛谷P4147] 玉蟾宫的更多相关文章

  1. [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)

    [BZOJ 3039&洛谷P4147]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. ...

  2. 洛谷P4147 玉蟾宫(动规:最大子矩形问题/悬线法)

    题目链接:传送门 题目大意: 求由F构成的最大子矩阵的面积.输出面积的三倍. 1 ≤ N,M ≤ 1000. 思路: 悬线法模板题. #include <bits/stdc++.h> us ...

  3. 洛谷P4147 玉蟾宫 单调栈/悬线法

    正解:单调栈/悬线法 解题报告: ummm这题我当初做的时候一点思路也没有只会暴力出奇迹:D(啊听说暴力好像能水过去呢,,, 然后当初是看的题解,然后学了下悬线法 然后就忘了:D 然后我现在看发现看不 ...

  4. 洛谷 P4147 玉蟾宫 (最大子矩形问题)

    这道题用到了悬线法,非常牛逼,可以看这个论文. https://blog.csdn.net/twtsa/article/details/8120269 #include<cstdio> # ...

  5. 洛谷P4147 玉蟾宫 (单调栈)

    要求我们去找一个最大矩形面积. 单调栈做法(和P1950 长方形那道题类似(一模一样)). 1 #include<bits/stdc++.h> 2 using namespace std; ...

  6. P4147 玉蟾宫--单调栈

    P4147 玉蟾宫 题目背景 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. 题目描述 这片土地被分成N*M个格子,每个格子 ...

  7. 悬线法 || BZOJ3039: 玉蟾宫 || Luogu P4147 玉蟾宫

    题面: P4147 玉蟾宫 题解:过于板子举报了 #include<cstdio> #include<cstring> #include<iostream> #de ...

  8. P4147 玉蟾宫

    P4147 玉蟾宫 给定一个 \(N * M\) 的矩阵 求最大的全为 \(F\) 的子矩阵 Solution 悬线法 限制条件为转移来的和现在的都为 \(F\) Code #include<i ...

  9. P4147 玉蟾宫(悬线法求最大子矩阵)

    P4147 玉蟾宫 悬线法 ,\(l_{i,j},r_{i,j},up_{i,j}\) 分别表示 \((i,j)\) 这个点向左,右,上能到达的远点.然后面积就很好办了.具体实现见代码. 然而,还有更 ...

随机推荐

  1. SpringBoot集成Apache Shiro

    笔者因为项目转型的原因,对Apache Shiro安全框架做了一点研究工作,故想写点东西以便将来查阅.之所以选择Shiro也是看了很多人的推荐,号称功能丰富强大,而且易于使用.实践下来的确如大多数人所 ...

  2. git更新提交代码常用命令

    git pull 拉取代码 git add -A 提交所有变化(包括删除.新增.修改) git commit -m "注释" 本地仓库提交 git push origin mast ...

  3. MyBatis模糊查询不报错但查不出数据的一种解决方案

    今天在用MyBatis写一个模糊查询的时候,程序没有报错,但查不出来数据,随即做了一个测试,部分代码如下: @Test public void findByNameTest() throws IOEx ...

  4. #Leetcode# 989. Add to Array-Form of Integer

    https://leetcode.com/problems/add-to-array-form-of-integer/ For a non-negative integer X, the array- ...

  5. c# winform导出Excel

    //需要注意添加引用Microsoft.Office.Interop.Excel.dll string fileName =DateTime.Now.Year+ DateTime.Now.Month+ ...

  6. MySQL 性能调优之SQL

    原文:http://bbs.landingbj.com/t-0-245451-1.html 对于SQL的优化,我们主要提供调整执行计划.优化SQL的方法有:缩短访问的路径.尽早过滤数据.尽可能减少排序 ...

  7. PHP--高级算法--面试

    数据结构和算法(转载) 原文地址:  https://blog.csdn.net/s1070/article/details/51174725 1.使对象可以像数组一样进行foreach循环,要求属性 ...

  8. 【学亮IT手记】jQuery each()函数用法实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  9. 通用模块设计UMD

    https://leohxj.gitbooks.io/front-end-database/content/javascript-modules/about-umd.html UMD(universa ...

  10. java.lang.Comparable 接口 详解

    参考https://blog.csdn.net/itm_hadf/article/details/7432782 http://www.blogjava.net/jjshcc/archive/2011 ...