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. 图解HTTP(二)

    第四章 返回结果的HTTP状体码 1.状态码告知从服务器返回的结果   类别 原因短语 1XX Infomational信息性状态码 接收的请求正在处理中 2XX Success成功状态码 请求正常处 ...

  2. 长期作业:web框架源码剖析

    Tornado框架 1.1. 手动安装 1.2. 从简单的开始:分析红框部分的源码 Django框架

  3. Maven打包时集成依赖项或复制依赖项到指定目录

    1.集成依赖项,最后生成的jar文件包含所有依赖: <build> <plugins> <plugin> <artifactId>maven-assem ...

  4. js计算hashcode

    String.prototype.hashCode = function(){ var hash = 0; for (var i = 0; i < this.length; i++) { var ...

  5. 四级CET大学词汇六级备份

    Cet6六级中要考到法庭词汇的小故事  如何安排六级考试前的一个月1.每天按照我的要求去背单词2.做四套真题,词汇部分 只做词汇 3.做personal dictionary把真题中出现的所有不认识的 ...

  6. 【OGG】OGG简单配置双向复制(三)

    [OGG]OGG简单配置双向复制(三) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O ...

  7. js 数组 深拷贝 复制 (汇总)

    https://www.cnblogs.com/zhoupengyi/p/6048955.html https://www.cnblogs.com/racyily/p/3532176.html htt ...

  8. 使用10046追踪执行计划demo

    (一)开启10046追踪 SQL> alter session set events '10046 trace name context forever,level 12'; (二)执行sql语 ...

  9. python测试开发django-43.xadmin添加小组件报错解决

    前言 xadmin首页上有个添加小组件按钮,打开的时候会报错“render() got an unexpected keyword argument 'renderer'”环境:python3.6dj ...

  10. gRPC应用C++

    1.  gRPC简述 RPC,远程方法调用,就是像调用本地方法一样调用远程方法. gRPC是Google实现的一种RPC框架,基于HTTP/2标准设计,带来诸如双向流.流控.头部压缩.单 TCP 连接 ...