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. python网络爬虫(三)requests库的13个控制访问参数及简单案例

    酱酱~小编又来啦~

  2. [Docker] 使用docker inspect查看宿主机与容器的共享目录

    docker inspect 容器名,可以查看到容器的元信息,在返回的j'son信息里面有个Mounts字段可以看到挂载目录 "Mounts": [ { "Type&qu ...

  3. 12-Factor与云原生Part2

    12-Factor与云原生Part2 12-Factor 为构建如下的 SaaS 应用提供了方法论: 使用声明式格式来搭建自动化,从而使新的开发者花费最少的学习成本加入这个项目 和底层操作系统保持简洁 ...

  4. php ip转换省市县

    http://www.cz88.net/ip/ http://www.ttlsa.com/php/php_cunzhen-ipdata/ # wget h http://6.scdx3.crsky.c ...

  5. Node.js核心模块-net

    net.Socket 类 socket.remotePort 访问服务器的远程端口 const http = require('http'); const server = http.createSe ...

  6. Excel——排序筛选

    1,自定义排序:多个关键字,从右向左一一排序 * 按颜色排序 * 按自定义序列排序 *两列中,列一个中间数,升序 * 打印标题行 * 选中,定位条件(可见),选择 * 数值筛选(大于等于),文本筛选( ...

  7. Electron – 基础学习(2): 项目打包成exe桌面应用 之electron-packager

    项目创建完成,启动正常,接下来就是项目打包了.将测试Demo打包成exe桌面应用,点击exe文件,运行项目. 书接上文,创建项目有三种方式 Git拷贝.直接创建:通过electron社群提供的命令行工 ...

  8. yii 日志和事件

    日志 配置 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log ...

  9. Mac brew update 慢~~~

    正题开始之前, 如果对 Homebrew 不太了解, 这里有一篇很好的介绍文章: macOS 包管理工具 Homebrew 不完全指南 花个几分钟读一下, 绝对超值! 正题 Homebrew 通过 G ...

  10. Markdown数学公式如何打出回归符号

    来源:https://blog.csdn.net/garfielder007/article/details/51646604 函数.符号及特殊字符 语法 效果 语法 效果 语法 效果 \bar{x} ...