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. 2018.11.14 hibernate中的查询优化---关联级别查询

    查询优化------关联级别查询 集合策略 在Mapper映射文件中添加属性 测试数据 lazy:true 延时加载数据 fetch:select 单表查询 控制台显示输出 结论:单表查询,使用到在加 ...

  2. 2018.10.4 AndroidStudio

    AndroidStudio低版本sdkversion开发 Error:Minimum supported Gradle version is 4.1 Current version is 2.14.1 ...

  3. out 和ref 的区别

    练习 1: 练习 2: 练习 3:

  4. 【洛谷P4342】[IOI1998]Polygon

    Polygon 比较裸的环形DP(也可以说是区间DP) 将环拆成链,复制到后面,做区间DP即可 #include<iostream> #include<cstdio> usin ...

  5. Android学习笔记_JNI_c调用java代码

    1.编写native方法(java2c)和非native方法(c2java): package com.example.provider; public class CallbackJava { // ...

  6. GYM 101550 G.Game Rank(模拟)

    The gaming company Sandstorm is developing an online two player game. You have been asked to impleme ...

  7. tomcat的下载和启动

    1.下载和解压 把下载好的压缩包解压,放到想刚的目录里 看一下目录: 目录介绍: 2. 启动和访问 启动步骤: 如果startup.bat 双击执行脚本一闪而过,解决方法: 第一可能是:没有配置JAV ...

  8. U盘安装CentOS7.3教程

    0.准备工作: 一台没系统的普通电脑u盘一个(大于1G,最小安装的话不超过1G,根据选择系统大小匹配U盘即可)CentOS7.3 iso文件一个UltraISO工具 1.制作U盘 ①使用UltraIS ...

  9. C# 通过socket实现UDP 通信

    UDP不属于面向连接的通信,在选择使用协议的时候,选择UDP必须要谨慎.在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较严重.但是由于UDP的特性:它不属于连接型协议,因而具有资源消耗小, ...

  10. logistic regression svm hinge loss

    二类分类器svm 的loss function 是 hinge loss:L(y)=max(0,1-t*y),t=+1 or -1,是标签属性. 对线性svm,y=w*x+b,其中w为权重,b为偏置项 ...