Problem 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
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; int a[];
int l[],r[];
int main()
{
int n; while(scanf("%d",&n) && n != )
{
long long ans=;
memset(a,,sizeof(a));
for(int i = ;i<n;i++)
{
scanf("%d",&a[i]);
l[i] = r[i] = i;
while(l[i] > && a[l[i]-] >= a[i])
{
l[i] = l[l[i]-];
}
}
for(int i = n-;i>=;i--)
{
while(r[i]<n- && a[r[i]+] >= a[i])
{
r[i] = r[r[i]+];
}
}
for(int i = ;i<n;i++)
{
long long s = (long long)a[i]*(r[i]-l[i]+);
if(s>ans)
{
ans = s;
}
}
printf("%lld\n",ans);
}
return ;
}

Largest Rectangle in a Histogram【暑期培训Q题】【DP】【递归】

hdoj - 1506 直方图中最大的矩形的更多相关文章

  1. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  2. hdu1506 直方图中最大的矩形 单调栈入门

    hdu1506 直方图中最大的矩形 单调栈入门 直方图是由在公共基线对齐的矩形序列组成的多边形.矩形具有相同的宽度,但可能具有不同的高度.例如,左侧的数字显示了由高度为2,1,4,5,1,3,3的矩形 ...

  3. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. AcWing:131. 直方图中最大的矩形(贪心 + 单调栈)

    直方图是由在公共基线处对齐的一系列矩形组成的多边形. 矩形具有相等的宽度,但可以具有不同的高度. 例如,图例左侧显示了由高度为2,1,4,5,1,3,3的矩形组成的直方图,矩形的宽度都为1: 通常,直 ...

  5. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. 如何在html中做圆角矩形和 只有右边的"分隔线"

    这个网站满好的,可以常看看 css-matic中有几个很好的写css可视化的工具 其实做css 版式布局等都可以有工具的 推荐40个优秀的免费CSS工具 debugger正则表达式在线 其实是对(理论 ...

  7. CODESOFT中的圆角矩形的弧度该怎样设置?

      CODESOFT标签设计软件提供多种图形制作按钮,方便用户更为快捷的制作标签.其中就包括矩形,圆角矩形的快捷创建按钮.本文将介绍如何设置CODESOFT圆角矩形的弧度. 若有疑问可直接访问:htt ...

  8. hdoj 1506&amp;&amp;1505(City Game) dp

    // l表示从l[i]到i连续大于a[i]的最远左区间.r表示从i到r[i]连续大于a[i]的最远又区间 DP 找出 a[i] 的最远左区间和最远右区间与自己连着的比自己大的数的长度 , 然后用这个长 ...

  9. SQL Server 中统计信息直方图中对于没有覆盖到谓词预估以及预估策略的变化(SQL2012-->SQL2014-->SQL2016)

    本位出处:http://www.cnblogs.com/wy123/p/6770258.html 统计信息写过几篇了相关的文章了,感觉还是不过瘾,关于统计信息的问题,最近又踩坑了,该问题虽然不算很常见 ...

随机推荐

  1. CSS-盒模型与文本溢出笔记

    注意点: 文本居中: text-align:center:文本左右居中 line-heigh:30px;  等于容器高度时,单行文本上下居中 margin:0 auto: 浏览器居中 清除margin ...

  2. Flink入门 - API

    final StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment.getExecutio ...

  3. dexlib2的源码框架

    这个是dexlib2的目录,明显看出来比baksmali和smali代码量要多很多,这里先将核心目录给大家做一下介绍 analysis 这个暂时不知道具体作用 base     这个文件夹下面全部都是 ...

  4. 注入 Istio sidecar

    注入 Istio sidecar 网格中的每个 Pod 都必须伴随一个 Istio 兼容的 Sidecar 一同运行. 下文中将会介绍两种把 Sidecar 注入到 Pod 中的方法:使用 istio ...

  5. Yii2通过curl调用json-rpc接口

    Yii2可以通过json-rpc为前端提供接口数据,通常情况睛会使用异步的形式调用接口,有时也会使用curl调用接口数据. 一.异步调用json-rpc接口 $.ajax({ type: 'POST' ...

  6. 【HICP Gauss】数据库 数据库管理(shutdown 日志 连接命令)-5

    数据库关闭终止Zengine进程关闭数据库 会导致无法预料的状态 建议shutdown关闭数据库 shutdown 模式1.normal默认 停止新请求 断开等待会话 关闭服务 终止主进程 需要连接发 ...

  7. SHELL脚本编程-字符串处理

    SHELL脚本编程-字符串处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符串切片 [root@node101.yinzhengjie.org.cn ~]# echo { ...

  8. lock in share mode 和 select for update

    lock in share mode 和 select for update 2018年07月11日 01:57:58 道不虚行只在人 阅读数 146    版权声明:欢迎转载,请注明出处 https ...

  9. Python +appium logger

    封装日志模块 import logging def get_log(): # 配置日志参数 logger = logging.getLogger() logger.setLevel(logging.I ...

  10. 助教总结---继alpha版本1之后

    本周心得: 在项目的开发当中,学生难免会有懈怠的时候,作为助教更应该去督促和激励同学们,但本质上该对自己负责任的是同学们自己.同学们项目的第一版本已经出来了,这个过程他们自己知道付出了多少,相信他们体 ...