题目网址: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])。
 
代码:
 #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=;
int n,a[N];
int left[N],right[N];
int main(){
while (scanf("%d",&n)!=EOF && n) {
ll res=;
for (int i=; i<=n; i++) {
scanf("%d",&a[i]);
left[i]=i;
right[i]=i;
}
for (int i=; i<=n; i++) {
int j=i;
while (j>= && a[i]<=a[j]) j=left[j]-;//通过之前的遍历结果更快地找到区间,若是用j--;会超时
left[i]=j+;
}
for (int i=n-; i>=; i--) {
int j=i;
while (j<=n && a[i]<=a[j]) j=right[j]+;
right[i]=j-;
}
for (int i=; i<=n; i++) res=max(res,1LL*(right[i]-left[i]+)*a[i]);//转换为long long型
printf("%lld\n",res);
}
return ;
}

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. MySQL二进制日志分析-代码实现(FORMAT_DESCRIPTION_EVENT)

    如前文概述,MySQL Binlog v3以前版本, 二进制日志文件的第一个事件是START_EVENT_V3, 从v4版本开始第一个事件为FORMAT_DESCRIPTION_EVENT(以下简称F ...

  2. Springboot+Shiro+Mybatis+mysql

    一 .shiro框架 Shiro是Apache 的一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.Shiro 主要分为两个部分就是认证和授权两部分 1.Subject代表了当前 ...

  3. Linux 笔记 - 第十二章 Shell 脚本

    博客地址:http://www.moonxy.com 一.前言 常见的编程语言分为两类:一类是编译型语言,如:C.C++ 和 Java等,它们远行前要经过编译器的编译.另一类是解释型语言,不需要编译, ...

  4. 新手学习FFmpeg - 调用API完成两个视频的任意合并

    本次尝试在视频A中的任意位置插入视频B. 在上一篇中,我们通过调整PTS可以实现视频的加减速.这只是对同一个视频的调转,本次我们尝试对多个视频进行合并处理. Concat如何运行 ffmpeg提供了一 ...

  5. CDH健康检查报DATA_NODE_BLOCK_COUNT告警

    告警原文: The health test result for DATA_NODE_BLOCK_COUNT has become concerning: The DataNode has 500,0 ...

  6. Linux初识之VMWare中Centos7的安装

    Windows平台下VMWare 14安装Centos 7 一.虚拟机硬件配置 1.选择创建新的虚拟机: 2.选择自定义(高级)进行自定义配置,单击下一步: 3.选择虚拟机硬件兼容性为默认,单击下一步 ...

  7. centos下U盘重装windows

    当前机器使用的都是centos系统,后来想使用windows,这时候就需要在centos下去安装windows系统啦. 当然是采用U盘安装啦,先准备U盘,U盘数据先备份以免丢失. 第一步:制作U盘启动 ...

  8. 网关鉴权后下游统一filter获取用户信息

    1. 场景描述 最近有点忙,在弄微服务nacos+springcloud gateway这块工作,以前只是简单应用,这次因为要对接10几个系统或者平台,还的鉴权,等后续稍微闲点了,把这块东西总结下. ...

  9. windows无法安装到这个磁盘怎样解决,及激活

    在cmd输入.sql server 2008  slmgr.vbs -ipk KH2J9-PC326-T44D4-39H6V-TVPBY  这个问题遇到的挺多次的,依稀记得上次搞这个问题搞了很久,今天 ...

  10. django查询表记录的十三种方法

    django查询表记录的十三种方法 all() 结果为queryset类型 >>> models.Book.objects.all() <QuerySet [<Book: ...