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. Python进阶----SOCKET套接字基础, 客户端与服务端通信, 执行远端命令.

    Python进阶----SOCKET套接字基础, 客户端与服务端通信, 执行远端命令. 一丶socket套接字 什么是socket套接字: ​ ​  ​ 专业理解: socket是应用层与TCP/IP ...

  2. aria2 https

    https://github.com/aria2/aria2/issues/361 ... and also make sure that aria2 was built with HTTPS sup ...

  3. shell截取字符串操作

    举例变量:url=http://www.baidu.com/123456.html 1. # 号截取,删除左边字符,保留右边字符. echo ${url#*//} # 其中 url 是变量名,# 号是 ...

  4. 【开发笔记】- 永远不要在MySQL中使用UTF-8

    原文地址:https://mp.weixin.qq.com/s/I3Tkvn8vSyC5lEpD9HzwiA 最近我遇到了一个bug,我试着通过Rails在以“utf8”编码的MariaDB中保存一个 ...

  5. springboot feign too many bytes written executing POST

    解決办法: pom添加: <dependency><groupId>io.github.openfeign</groupId><artifactId>f ...

  6. GitHub Java项目推荐|功能丰富的 Java 工具包|提高开发效率

    GitHub Java项目推荐|功能丰富的 Java 工具包|提高开发效率 功能丰富的 Java 工具包.它帮助我们实现了常用的工具方法,从而减少代码的体积,提高开发效率.该项目最初是作者工作项目中的 ...

  7. Maven项目命名规范

    Guide to naming conventions on groupId, artifactId and versiongroupId will identify your project uni ...

  8. JavaScript: 详解正则表达式之一

    正则表达式是一个精巧的利器,经常用来在字符串中查找和替换,JavaScript语言参照Perl,也提供了正则表达式相关模块,开发当中非常实用,在一些类库或是框架中,比如jQuery,就存在大量的正则表 ...

  9. Four subspaces - Prof. Strang

    相应地,用映射的观点考察映射σ的kernal space与image space,得到的就是下面这张图啦 Ref: Introduction to Linear Algebra 5th Edition ...

  10. The Tower(HDU6559+2018年吉林站+数学)

    题目链接 传送门 题意 告诉你圆锥的底部圆的半径和圆锥的高,再给你一个点的坐标及其运动向量,问你这个点什么时候会与这个圆锥相撞. 思路 比赛场上二分一直没过但是有人二分过了,今天再写这题想再试下二分, ...