Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:


Usually, histograms are used to represent discrete distributions,
e.g., the frequencies of characters in texts. Note that the order of the
rectangles, i.e., their heights, is important. Calculate the area of
the largest rectangle in a histogram that is aligned at the common base
line, too. The figure on the right shows the largest aligned rectangle
for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For
each test case output on a single line the area of the largest
rectangle in the specified histogram. Remember that this rectangle must
be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

Hint

Huge input, scanf is recommended.

Source

 
思路:单调栈题。用一个deque代替栈来模拟。记录每个矩形长度为1,用一个totlen保存当前所有矩形的长度,枚举每个矩形的高度h,维护最大面积maxx。
有一个关键的对maxx的更新在于碰到不单调情况时,一直往左边pop找到一个小于当前读入的矩形高度的合法值,这个pop掉的矩形是一个单独凸起的高度的,需要单独记录一次面积。
最后,若队列中仍有元素,更新一遍maxx。
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque> using namespace std;
typedef __int64 LL;
const int maxn=1e5+7; LL maxx; struct node{
LL len;
LL h;
}a[maxn]; int main()
{
LL n;
while(~scanf("%I64d",&n)&&n){
for(LL i=0;i<n;i++)
{
scanf("%I64d",&a[i].h);
a[i].len=1;
}
deque <node> q;
q.push_back(a[0]);
maxx=0;
for(LL i=1;i<n;i++)
{
if(a[i].h>=a[i-1].h)
{
q.push_back(a[i]);
}
else
{
LL totlen=0;
LL ae=0;
while(!q.empty()&&a[i].h<q.back().h)
{
totlen+=q.back().len;
ae=totlen*q.back().h;
maxx=max(maxx,ae);
q.pop_back();
}
totlen+=a[i].len;
a[i].len=totlen;
q.push_back(a[i]);
}
} LL totlen=0,ae=0; while(!q.empty())
{
totlen+=q.back().len;
ae=totlen*q.back().h;
maxx=max(maxx,ae);
q.pop_back();
}
printf("%I64d\n",maxx);
}
return 0;
}

HDU-1506 Largest Rectangle in a Histogram【单调栈】的更多相关文章

  1. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  2. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  3. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  4. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  5. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

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

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

  7. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  8. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. HDU 1506 Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

随机推荐

  1. 子组件获取父组件数据 propsDown, 父组件获取子组件数据 eventUp

    (一) popsDowm 三种方法获取父组件数据:被动获得(1):主动获取(2). 1.被动获得: 父组件:v-bind: 绑定变量参数和方法参数:子组件:props 接收参数.可以在模板中直接使用也 ...

  2. Java 中常见排序算法

    经典的排序算法总结 冒泡排序算法 算法描述: 比较相邻的元素:如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针 ...

  3. Linux学习Day3:新手必须掌握的Linux命令(二)

    今天学习的命令都是运维工作中经常要用到的,非常实用,必须要用心学习,争取把这些命令烂熟于心,具体内容如下: 一.系统状态监测命令 1.ifconfig命令 用于获取网卡配置与网络状态等信息. [roo ...

  4. 单源最短路 Bellman-Ford算法(有向图)

    // 单源最短路问题 // Bellman-Ford算法 // 复杂度O(V*E) //! 可以判断负圈 #include <cstdio> #include <iostream&g ...

  5. clr via c# delegate

    1,委托列子 internal delegate void Feedback(int value); class DelegateRef { public static void StaticDele ...

  6. 快速建立使用django_rest_framework

    快速建立使用django_rest_framework 测试API 其实djangorestframework就是进行增删改查 前往要注意的是 POST的接口的的URL一定要写完整 包括最后的 `/` ...

  7. VS GIT 使用入门---我只是搬运工

    网上资料推荐 GitHub 新手详细教程 - Hanani_Jia的博客 - CSDN博客 https://blog.csdn.net/Hanani_Jia/article/details/77950 ...

  8. 初识Socket通讯编程(一)

    一.什么是socket? 当两台计算机需要通信的时候,往往我们使用的都是TCP去实现的,但是并不会直接去操作TCP协议,通常是通过Socket进行tcp通信.Socket是操作系统提供给开发者的一个接 ...

  9. springboot打成war包并携带第三方jar包

    1.修改打包方式为war     <packaging>war</packaging> 2.添加第三方依赖的jar到pom     我的第三方jar包在resoueces目录下 ...

  10. Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

    Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...