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. 【洛谷P1169】[ZJOI2007]棋盘制作

    棋盘制作 题目链接 这个题是[USACO5.3]巨大的牛棚Big Barn和玉蟾宫的结合 一道顶两道毒瘤! 题解: 首先,棋盘有两种选法: 1.任意白格(x,y) (x+y)%2=0 ,任意黑格(x, ...

  2. 使用带有对象的data-ng-bind

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. Python使用dict和set

    dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也成为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的 ...

  4. UDP端口启动后一段时间无法接收到数据

    接口需求:开发一个UDP协议的接口作为服务端接收来自客户端的认证数据,数据量每分钟7w+条; 数据格式:标准的redius协议,redius协议的相关知识在网上查资料,提供线索:http://blog ...

  5. npm ERR! code: 'EPERM' (权限问题 errro permit)

    PS C:\Users\user\Desktop\test\my-project> npm run iview --save npm ERR! missing script: iview npm ...

  6. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--F-等式

    链接:https://www.nowcoder.com/acm/contest/90/F 来源:牛客网 1.题目描述 给定n,求1/x + 1/y = 1/n (x<=y)的解数.(x.y.n均 ...

  7. 内存分配---FF、BF、WF三种算法

    动态分区分配是根据进程的实际需要,动态的为之分配内存空间.而在实现可变分区分配时,将涉及到分区分配中 所用的数据结构.分区分配算法和分区的分配与内存回收的过程. 分区分配中的数据结构:(1)描述空闲块 ...

  8. ABAP术语-ISO (International Organization for Standardization)

    ISO (International Organization for Standardization) 原文:http://www.cnblogs.com/qiangsheng/archive/20 ...

  9. shiro笔记-"Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - ylw, rememberMe=false]. Possible unexpected error? (Typical or expected login exceptions should ext

    在学习shiro过程中遇到这个错误,在网上找了好久资料也没找到解决办法,大概都是说和传入的值有问题.于是我试着耐心看我自己的报错信息,最终找到了原因并解决.每个人的问题可能都会有差异,所以建议大家耐心 ...

  10. 高级同步器:交换器Exchanger

    引自:https://blog.csdn.net/Dason_yu/article/details/79764467 一.定义每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹 ...