Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15189    Accepted Submission(s): 4408

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>
#define Max 100005
__int64 dpl[Max],dpr[Max],h[Max];
int main()
{
__int64 n,k,m;
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%I64d",&n)!=EOF)
{
if(n==)
break;
m=;
for(i=;i<=n;i++)
{
scanf("%I64d",&h[i]);
dpr[i]=dpl[i]=i;
}
h[n+]=h[]=;
for(i=n;i>=;i--)
{
while(h[dpr[i]+]>=h[i])
dpr[i]=dpr[dpr[i]+];
}
for(i=;i<=n;i++)
{
while(h[dpl[i]-]>=h[i])
dpl[i]=dpl[dpl[i]-];
}
for(i=;i<=n;i++)
{
k=(dpr[i]-dpl[i]+)*h[i];
if(m<k) m=k;
}
printf("%I64d\n",m);
}
return ;
}

Largest Rectangle in a Histogram(HDU 1506 动态规划)的更多相关文章

  1. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  2. Day8 - C - Largest Rectangle in a Histogram HDU - 1506

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  3. V - Largest Rectangle in a Histogram HDU - 1506

    两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...

  4. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  5. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  6. 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 ...

  7. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  8. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  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. Objective-C Http常用API 同步请求与异步请求

    开发iOS应用要调用Http接口.获取Http资源,有一套比较成熟的框架ASIHTTPRequest.而我还是比较喜欢使用原始一点的 API,而它跟其他的面向对象语言有许多共通之处.本文分同步请求和异 ...

  2. Visual studio 内存不足的解决方案(out of memory)

    编译Visual Studio项目,如果出现"out of memory "的编译错误,可以进行如下操作,加大应用程序可以使用的内存. 请先备份好系统和设置好系统还原点,大体步骤是 ...

  3. AngularJS自定义表单验证

    <!doctype html> <html ng-app="myApp"> <head> <script src="G:\\So ...

  4. 使用redis缓存加索引处理数据库百万级并发

    使用redis缓存加索引处理数据库百万级并发 前言:事先说明:在实际应用中这种做法设计需要各位读者自己设计,本文只提供一种思想.准备工作:安装后本地数redis服务器,使用mysql数据库,事先插入1 ...

  5. 变量数据是怎么进ARM中的RAM中?

    这篇文章 是从网上复制过来的.觉得不错,分享给大家.原文地址:http://eetrend.com/forum/100028828 ARM的体系结构有很多很多介绍的地方,从其7种模式到CPSR状态寄存 ...

  6. 使用 ExpandableListView 实现折叠ListView

    1:layout/expandablelistview_groups.xml 标题文件 <?xml version="1.0" encoding="utf-8&qu ...

  7. CentOS下Qt窗口透明效果失效,成黑色背景的问题

    一.问题 Linux系统下Qt窗口的透明效果成了黑色,但同样的代码在windows机子上有透明效果,主要是修改系统的配置,仅在centos6.3及其以上版本实验过.其他系统可以相应配置. 二.问题运行 ...

  8. cf493A Vasya and Football

    A. Vasya and Football time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. CCF软考---《有趣的数》

    脑子一热报了CCF的软测..但是又觉得好像并没有什么卵用,就当为蓝桥杯预热然后顺便去软件学院玩一玩吧,遇到一个有意思的题: time limits : 1s 问题描述 我们把一个数称为有趣的,当且仅当 ...

  10. 又一编辑神器-百度编辑器-Ueditor

    (Lionden<hsdlionden@gmail.com> 转载说明) 前段时间发表过一篇关于“KindEditor在JSP中使用”的博文.这几天在沈阳东软进行JavaWeb方面的实习工 ...