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<stdio.h>
int r[], l[];
long long s[];
int main() {
int n, i, j;
long long ans, temp;
while (scanf("%d", &n), n) {
for (i = ; i <= n; i++) {
scanf("%lld", &s[i]);
l[i] = r[i] = i;
}
s[n + ] =s[] = -;
for (i = ; i <= n; i++) {
while (s[l[i] - ] >= s[i])
l[i] = l[l[i] - ];//因为有了这层数组嵌套,才能体现动态规划
} //的优点,表示l[i]是在前一个的基础上得到的,那下次就
for (i = n; i >= ; i--) { //不用再重复计算前一个了,数据多的时候,
while (s[i] <= s[r[i] + ]) //可以节省很多时间
r[i] = r[r[i] + ];
}
ans = ;
for (i = ; i <= n; i++) {
temp = s[i]*(r[i] - l[i] + ) ;
if (temp > ans)ans = temp;
}
printf("%lld\n", ans);
}
return ;
}

hdu1506 Largest Rectangle in a Histogram的更多相关文章

  1. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. hdu1506——Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. HDU1506 Largest Rectangle in a Histogram (动规)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!

                                         Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...

  5. 【题解】hdu1506 Largest Rectangle in a Histogram

    目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...

  6. 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. HDU1506 ( Largest Rectangle in a Histogram ) [dp]

    近期情绪太不稳定了.可能是由于在找实习这个过程碰壁了吧.第一次面试就跪了,可能是我面的是一个新公司,制度不完好,我感觉整个面试过程全然不沾编程,我面试的还是软件开发-后来我同学面试的时候.说是有一道数 ...

  8. 【单调栈】hdu1506 Largest Rectangle in a Histogram

    单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是 ...

  9. HDU1506 Largest Rectangle in a Histogram(算竞进阶习题)

    单调栈裸题 如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可 当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来 ...

随机推荐

  1. POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】

    题目传送门:http://poj.org/problem?id=3764 The xor-longest Path Time Limit: 2000MS   Memory Limit: 65536K ...

  2. [19/03/23-星期六] 容器_ 泛型Generics

    一.概念 生活中的容器不难理解,是用来容纳物体的,程序中的“容器”也有类似的功能,就是用来容纳和管理数据. 数组就是一种容器,可以在其中放置对象或基本类型数据. ---优势:是一种简单的线性序列,可以 ...

  3. 2018.11.28 OGNL表达式与struts2框架结合的体现---在配置文件中体现(补充)

    Demo3Action配置 struts.xml 主配置文件配置 地址栏输入 http://localhost:8080/StrutsDay03/Demo3Action 对于主配置的文件参数而言,如果 ...

  4. Spring 声明式事务管理方式

    声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例)      接口Service public i ...

  5. 八、IntelliJ IDEA 缓存和索引的介绍及清理方法

    这样一句话“ 对于首次创建或打开的新项目,IntelliJ IDEA 都会创建项目索引,大型项目在创建索引的过程中可能会出现卡顿的现象,因此强烈建议在 IntelliJ IDEA 创建索引的过程中不要 ...

  6. 【luogu P4231 三步必杀】 题解

    题目链接:https://www.luogu.org/problemnew/show/P4231 诶 我很迷啊..这跟树状数组有什么关系啊...拿二阶差分数组过了..? #include <cs ...

  7. 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay

    题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...

  8. java递归菜单树转换成pojo对象

    package com.cjonline.foundation.authority.pojo; import java.util.ArrayList; import java.util.Collect ...

  9. EF6.0 对于数据库优 模式 新加功能

    EF6.0相对于5.0新加了很多功能.先看看两个模式的一些特点. 数据库优先(设计者)和代码优先两者的特点: 连接弹性 异步查询和保存 基于代码的配置 数据库命令记录 数据库命令截取 依赖决议 DbS ...

  10. Swift 中关于”??”操作符

    Swift 中关于”??”操作符 Swift 的语法在保证安全和健壮的基础上,又带有很多非常灵活的特性,比如 ?? 操作符就是其中一个.大家可能已经了解它,也可能有些同学不了解它,这里给大家整理了关于 ...