浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html

题目传送门:http://poj.org/problem?id=2559

贪心的想,最大的子矩阵顶部肯定会与某个矩阵的顶部重合,所以我们可以考虑对于每个矩阵,如果一个子矩阵的高度与它相同,那么向左向右分别可以延伸多长即可。用单调栈维护,从前往后从后往前扫两遍统计答案即可。

时间复杂度:\(O(n)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const int maxn=1e5+5; ll ans;
int n,top;
int a[maxn],stk[maxn],sum[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} int main() {
while(1) {
n=read();
if(!n)break;top=ans=0;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
a[i]=read();
a[++n]=0;
for(int i=1;i<=n;i++) {
while(top&&a[stk[top]]>a[i])sum[stk[top]]+=i-stk[top]-1,top--;
stk[++top]=i;
}top=0;
for(int i=n-1;~i;i--) {
while(top&&a[stk[top]]>a[i])sum[stk[top]]+=stk[top]-i-1,top--;
stk[++top]=i;
}
for(int i=1;i<=n;i++)
ans=max(ans,1ll*a[i]*(sum[i]+1));
printf("%lld\n",ans);
}
return 0;
}

POJ2559:Largest Rectangle in a Histogram的更多相关文章

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

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

  2. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  3. HDU1506: Largest Rectangle in a Histogram(最大子矩阵,好题动态优化左右边界)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. ...

  4. [dp]POJ2559 && HDOJ1506 Largest Rectangle in a Histogram

    题意 给n个条形的高度, 问能放的最大矩形面积 分析: 从左到右 从右到左 各搞一遍 分别记录      L[i]记录列(从前往后)标 第几列开始 可以往后放高度为a[i]的矩形  R[i]记录列(从 ...

  5. POJ2559/HDU1506 Largest Rectangle in a Histogram (cartesian tree)

    Die datenstruktur ist erataunlich! #include <iostream> #include <cstdio> #include <cs ...

  6. 【题解】Largest Rectangle in a Histogram [SP1805] [POJ2559]

    [题解]Largest Rectangle in a Histogram [SP1805] [POJ2559] [题目描述] 传送: \(Largest\) \(Rectangle\) \(in\) ...

  7. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  8. 第一周 Largest Rectangle in a Histogram

    Language: 题目: Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. Largest Rectangle in a Histogram【单调栈模板】

    Largest Rectangle in a Histogram 题目链接(点击)来源poj 2559 A histogram is a polygon composed of a sequence ...

随机推荐

  1. Django之restframework2视图三部曲

    视图三部曲 下面我来来看restframework是如何将冗余的代码一步步的进行封装. 这里主要用到的是多继承 第一步mixin类编写视图 AuthorModelSerializer: class A ...

  2. hdoj 1455 Sticks 【dfs】

    题意:找最短的木棍可以组成的长度, hdoj  1518 的加强版 代码: #include <stdio.h> #include <string.h> #include &l ...

  3. python cookbook第三版学习笔记十七:委托属性

    我们想在访问实例的属性时能够将其委托到一个内部持有的对象上,这经常用到代理机制上 class A:     def spam(self,x):         print("class_A: ...

  4. zipfile.BadZipFile: File is not a zip file

    zipfile.BadZipFile: File is not a zip file 出现这个问题一般是文件损坏的可能性比较大

  5. pyspark

    http://www.aboutyun.com/thread-18150-1-1.html

  6. 【leetcode刷题笔记】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. STM32 MCO时钟输出配置实验

    STM32的PA.8引脚具有复用功能——时钟输出(MCO), 该功能能将STM32内部的时钟通过PA.8输出. 操作流程: 1).设置PA.8为复用AF模式. RCC_AHB1PeriphClockC ...

  8. Ansible playbook练习

    示例1:创建用户的Playbook --- - name: create user hosts: openstack gather_facts: false tasks: - name: create ...

  9. find查找文件

    linux下最强大的搜索命令为”find“. 它的格式为”find <指定目录> <指定条件> <指定动作>“: 比如使用find命令搜索在根目录下的所有inter ...

  10. expr 数字操作

    expr 可以用于计算 [root@rhel6 script]# * * [root@rhel6 script]# 使用expr来判断输入的变量是否为整数, 注意这里的&表示  安静模式(没有 ...