题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506

题目:

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18833    Accepted Submission(s): 5636

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 usedto 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
 
思路:
循环遍历每个小矩形,找到它左边连续的不小于他高度的矩形下标left[i],找到它右边连续的不小于他高度的矩形下标right[i]。
则若干小矩形形成的大矩形宽为right[i]-letf[i]+1,面积就等于(right[i]-left[i]+1)*a[i],a[i]为当前遍历的到的小矩形高度。
结果:res=max(res,(right[i]-left[i]+1)*a[i])。
 
代码:
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. typedef long long ll;
  5. const int N=;
  6. int n,a[N];
  7. int left[N],right[N];
  8. int main(){
  9. while (scanf("%d",&n)!=EOF && n) {
  10. ll res=;
  11. for (int i=; i<=n; i++) {
  12. scanf("%d",&a[i]);
  13. left[i]=i;
  14. right[i]=i;
  15. }
  16. for (int i=; i<=n; i++) {
  17. int j=i;
  18. while (j>= && a[i]<=a[j]) j=left[j]-;//通过之前的遍历结果更快地找到区间,若是用j--;会超时
  19. left[i]=j+;
  20. }
  21. for (int i=n-; i>=; i--) {
  22. int j=i;
  23. while (j<=n && a[i]<=a[j]) j=right[j]+;
  24. right[i]=j-;
  25. }
  26. for (int i=; i<=n; i++) res=max(res,1LL*(right[i]-left[i]+)*a[i]);//转换为long long型
  27. printf("%lld\n",res);
  28. }
  29. return ;
  30. }

HDU 1506 Largest Rectangle in a Histogram(区间DP)的更多相关文章

  1. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  2. HDU 1506 Largest Rectangle in a Histogram【DP】

    题意:坐标轴上有连续的n个底均为1,高为h[i]的矩形,求能够构成的最大矩形的面积. 学习的别人的代码 @_@ 看底的坐标怎么找的看了好一会儿--- 记l[i]为矩形的底的左边的坐标,就将它一直向左扩 ...

  3. hdu 1506 Largest Rectangle in a Histogram(DP)

    题意: 有一个柱状图,有N条柱子.每一条柱子宽度都为1,长度为h1...hN. 在这N条柱子所构成的区域中找到一个最大面积,每平方米3块钱,问最多赚多少钱. 输入: 1<=N<=10000 ...

  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. 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 ...

  7. 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 ...

  8. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  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 ...

随机推荐

  1. 数据库高级:SQL-CREATE-TABLE语句

    作者:松软科技(www.sysoft.net.cn) 发布时间:2019/3/17 9:34:51 CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CRE ...

  2. charles 端口转发

    本文参考:charles 端口转发 端口转发 端口转发(Port forwarding),有时被叫做隧道,是安全壳(SSH) 为网络安全通信使用的一种方法.端口转发是转发一个网络端口从一个网络节点到另 ...

  3. SpringCloud-Hystrix Dashboard 之 Unable to connect to Command Metric Stream

    实践hystrix dashboard仪表盘的时候,不管是按照书上的还是网上的,都提示Unable to connect to Command Metric Stream. 查了好久发现,如果使用sp ...

  4. Ubuntu python-matplotlib安装couldn't connect to display ":0.0"

    先卸载旧的matplotlib库(sudo pip3 uninstall matplotlib),再利用命令 sudo apt-get install python-matplotlib安装,自动安装 ...

  5. 【Java】SpringBoot 中从application.yml中获取自定义常量

    由于这里我想通过java连接linux,connection连接需要host.port.username.password及其他路径等等.不想每次修改的时候都去改源文件,所以想写在applicatio ...

  6. 45道CSS基础面试题(附答案)

    1 .介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content)+ border + padding + margin低版本IE盒子模型:宽 ...

  7. SpringBoot定时任务,总有一款适合你

    title: SpringBoot定时任务,总有一款适合你 date: 2019-09-28 16:19:10 tags: - springboot - 定时任务 categories: java - ...

  8. 一起来学Java注解(Annotation)

    目录 一. 什么是Annotation 二. Annotation的作用 2.1 编译器使用到的注解 2.2 .class文件使用到的注解 2.3 运行期读取的注解 三. 定义Annotation 3 ...

  9. flask+阿里云短信服务实现注册发送手机验证码

    效果图: 该效果主要讲解实现通过调用阿里云的SDK实现发送注册验证码短信(阿里云短信付费使用) 购买阿里云短信服务 购买链接:https://www.aliyun.com/product/sms 1. ...

  10. MySql 8.0.12安装、配置

    1. 参考:① 菜鸟教程下载安装MySQl ② 8.0.12安装方法 以下是我遇到的问题: 2.执行 mysqd --initialize --console 后,这个时候运行突然报"无法启 ...