NOIP模拟dp专题 Question 单调栈
来源:互联网 发布:幻想降临现实知轩 编辑:程序博客网 时间:2024/06/05 16:01
说实话这是我第一次用单调栈,也算是第一次了解单调栈。还真的是如果没做这道题我直到NOIP也不会了解这种数据结构。
题意
简而言之就是求01矩形内的最大矩形面积。
解法
先用dp求出h[i][j] 意为从(i,j)点向上有多少个连续的1(也可看作矩形的高)
接着用两个单调栈维护每一行,一个是从左往右扫递增,一个是从右往左扫递增(也就是从左往右扫递减)。
以ris[ ](左往右扫递增的栈)举例,点(i,j)在单调栈里的前一个点就是在它右边刚好比它小的第一个点,设这个点为(i,j1) 那么j-j1+1就是以i行为底h[i][j]为高矩形向右的最长宽度。
dwn[i][j]同理
#include<cstdio>#include<cstring>#include<vector>using namespace std;const int N=1005;typedef pair<int,int> pii;int n,m,a[N][N],h[N][N],top,len[N][N];pii ris[N],dwn[N];int main(){ freopen("question.in","r",stdin); freopen("question.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); if(a[i][j]) h[i][j]=h[i-1][j]+1; } for(int i=1;i<=n;i++){ top=0; ris[0]=make_pair(0,0); for(int j=1;j<=m;j++){ if(!top||ris[top].first<h[i][j]) { ris[++top]=make_pair(h[i][j],j); len[i][j]=1; } else { while(ris[top].first>=h[i][j]&&top) top--; len[i][j]=j-ris[top].second; ris[++top]=make_pair(h[i][j],j); } } top=0; for(int j=m;j>=1;j--){ if(!top||dwn[top].first<h[i][j]) dwn[++top]=make_pair(h[i][j],j); else{ while(dwn[top].first>=h[i][j]&&top) top--; if(top) len[i][j]+=dwn[top].second-j-1; else len[i][j]+=m-j; dwn[++top]=make_pair(h[i][j],j); } } } int ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans=max(ans,len[i][j]*h[i][j]); printf("%d",ans); return 0;}
阅读全文
0 0
- NOIP模拟dp专题 Question 单调栈
- 【DP】【单调栈】question 题解
- 10.11 NOIP模拟赛 DP + 线段树 + DP + 单调队列
- NOIP模拟 10.17 单调队列 + 树形Dp + 区间Dp
- NOIP模拟赛 军训(二分答案+单调队列优化DP)
- NOIP模拟:最佳序列(单调队列DP)
- question (单调栈)
- NOIP 模拟赛 最大子矩形 单调栈
- NOIP模拟题 [SPFA][DP][栈结构]
- [NOIP 模拟]潜水员 DP
- 【NOIp模拟】【dp】俄罗斯方块
- noip模拟赛财富(单调队列)
- [NOIP模拟题][DFS][DP]
- NOIP模拟题[dfs][DP]
- [NOIP模拟][状压DP]乘积
- NOIP模拟 探险 【树形dp】
- 【NOIP数据结构专项】单调队列单调栈
- 【专题】单调队列/斜率优化DP
- Java中命令行可以编译成功,但不能运行,报找不到或无法加载主类
- http请求测试实例(采用fastjson解析)
- 检查型异常(Checked Exception)与非检查型异常(Unchecked Exception)
- 2016蓝桥杯决赛 机器人塔(深搜DFS)
- js基础
- NOIP模拟dp专题 Question 单调栈
- 数据结构JAVA-----直接插入排序
- Python 实现比较文件内容异同
- STL-函数对象适配器
- Markdown新手指南手册
- Unix——optarg与getopt的用法
- Python中常用的模块--Log日志模块
- Android 权限及7.0权限的兼容
- 2017061801公有继承保护继承私有继承公有继承保护继承私有继承