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. DateTime小综合

    实现效果: 关键知识: 1>DateTime类的ToString()方法: 2>DateTime类的IsLeapYear(); 3>DateTime类的DaysInMomth(); ...

  2. 【转】Java虚拟机类型卸载和类型更新解析

    [摘要]         前面系统讨论过java类型加载(loading)的问题,在这篇文章中简要分析一下java类型卸载(unloading)的问题,并简要分析一下如何解决如何运行时加载newly ...

  3. 梯度下降(HGL)

    线性回归:是利用数理统计中回归分析,来确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法. 对于一般训练集:                参数系统为:      线性模型为:      损 ...

  4. Linux 下的多线程编程(1)

    #include<stdio.h> #include<pthread.h> #include<string.h> #include<sys/time.h> ...

  5. 使用补丁破解IntelliJ IDEA 2017收费版本(转)

    1. 首先去官网http://www.jetbrains.com/idea/download/#section=windows下载Ultimate版(注意不是community版)下载并安装.一定要记 ...

  6. VUE插件整理

    转自:https://blog.csdn.net/miaozhenzhong/article/details/80138174 1.VsCode官方插件地址: https://marketplace. ...

  7. WebGL学习笔记(3)

    根据上篇笔记,在对3D对象可进行普通的控制后,以及学习了http://hiwebgl.com的教程第10章内容:世界模型的载入以及控制镜头移动,经过多次调试矩阵代码,已经可以实现在世界中旋转镜头/控制 ...

  8. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  9. 在C++中如何实现文件的读写

    一.ASCII 输出为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取代< fstream.h>,所 ...

  10. Win10 VirtualBox虚拟机搭建lnmp环境

    之前用的是vagrant+VirtualBox搭建的环境,因为是windows系统动不动就报错,打不开环境,所以还是老老实实换了虚拟机哎.... 版本: VirtualBox 5.1.34   xsh ...