HDU-1506 Largest Rectangle in a Histogram【单调栈】
Description

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
Output
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
Source
#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【单调栈】的更多相关文章
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram(区间DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...
- 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 ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- 「C++ 」借来的资源,何如还的潇洒?
前言 本文的内容将专门对付内存管理,培养起有借有还的好习惯,方可消除资源管理的问题. 正文 所谓的资源就是,一旦用了它,将来必须还给系统.如果不是这样,糟糕的事情就会发生. C++ 程序内常见的资源: ...
- 一种高灵敏度自带DSP降噪算法的音频采集解决方案
背景调研 随着AI渗透到各行各业,人们对语音的需求也越来越大,最近一两年,各种AI音频设备如雨后春笋般冒出.各种智能AI设备的推出,意味者市场对低成本的音频采集设备越来越多.针对这种情况,我们开发 ...
- Linux下通过二进制方式安装mysql5.7版本和系统优化
本文主要介绍MySQL二进制软件包的安装/启动/关闭过程. 也许有人要问为什么要选择二进制的安装方式呢? 其实答案很简单,官方版本中已经把所有功能都配置好了,我们可以很方便地拿来使用. 官方MySQL ...
- MongoDB集群负载不均衡问题定位及解决
1.问题描述 这是一套运行在腾讯云上的MongoDB 3.6版本集群,共5个分片,每片规格是6核16GB. 在压测的过程中,发现第3个分片的CPU使用率长时间高达96%,其它4个分片的CPU使用率都没 ...
- MySQL 什么是事务?
该文为< MySQL 实战 45 讲>的学习笔记,感谢查看,如有错误,欢迎指正 一.事务简介 事务就是为了保证一组数据库操作,要么全部成功,要么全部失败. 事务是在引擎层实现的,也就是说并 ...
- 【算法】蓝桥杯 试题 基础练习 Huffuman树
资源限制 时间限制:1.0s 内存限制:512.0MB 问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, ...
- 得心应用的Vue高级技巧---vue中文社区
1,require.context()一个webpack的api,通过执行require.context函数获取一个特定的上下文,主要用来实现自动化导入模块,在前端工程中,如果遇到从一个文件夹引入很多 ...
- opencv —— Sobel 一阶导数算子、Scharr 滤波器 一阶导数用于边缘检测
sobel 算子的基本概念 sobel 算子是一个主要用于边缘检测的离散微分算子,它结合了高斯平滑和微分求导,用于计算图像灰度函数的近似梯度. 其基础来自于一个事实,即在边缘部分,像素值出现“跳跃”或 ...
- MQ日常命令
假设队列管理器为QMgrName,以下所有使用QMgrName的地方您都可以替换成您维护的mq队列管理器名称. 一.MQ的启动与停止 用root用户启/停需要root用户包含在mqm组中. 1.MQ的 ...
- ajax请求携带cookie和自定义请求头header
参考链接:https://blog.csdn.net/menghuanzhiming/article/details/102736312