题目网址: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. Kali Linux 安装open-vm-tools

    Kali Linux是基于Debian的Linux发行版,集成了精心挑选的渗透测试和安全审计的工具,供渗透测试和安全设计人员使用.(以及一些各种颜色的hacker  ^-^) 首先需要安装好虚拟机(V ...

  2. Java String 对象,你真的了解了吗?

    String 对象的实现 String对象是 Java 中使用最频繁的对象之一,所以 Java 公司也在不断的对String对象的实现进行优化,以便提升String对象的性能,看下面这张图,一起了解一 ...

  3. Java 截取字符串中指定数据及之后数据

    String resCallBackJson="12556{1{{{456858585{";        resCallBackJson = resCallBackJson.su ...

  4. 利用Jenkins实现项目自动化部署

    1.安装Jenkins,参考上一篇博客:安装Jenkins 安装Java 安装tomcat 安装maven 2.全局工具配置,填写好后点击save 3.安装git plugin插件

  5. [Advanced Python] 11 - Implement a Class

    基础概念:[Python] 08 - Classes --> Objects 进阶概念:[Advanced Python] 11 - Implement a Class 参考资源:廖雪峰,面向对 ...

  6. Android Studio [Activity的生命周期]

    package com.xdw.a122; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; imp ...

  7. Entity Framework Core生成的存储过程在MySQL中需要进行处理及PMC中的常用命令

    在使用Entity Framework Core生成MySQL数据库脚本,对于生成的存储过程,在执行的过程中出现错误,需要在存储过程前面添加 delimiter // 附:可以使用Visual Stu ...

  8. redis-分布式锁-消除竞争条件

    因为信号量的设计过程中,获取一个信号量需要执行多个命令组成的流水,这样容易形成竞争条件. 为了消除信号量实现中所有可能出现的竞争条件,构建一个正确的计数信号量,需要在 信号量时,添加带有短暂超时时间的 ...

  9. Linux 常用解压和压缩命令

    .tar 解包 tar xvf filename.tar.tar 打包 tar cvf filename.tar dirname.gz 解压1 gunzip filename.gz.gz 解压2 gz ...

  10. 在创建activiti5..22所需的25张表时 ,所用的方法和遇到的问题。

    最近在学习关于activiti流程设计的相关内容,首先第一步就需要了解25张activiti相关的表,具体的每张表的含义 请自行百度. 这里讲一下 用java代码生成所需要的25张表,很简单: pub ...