Largest Rectangle in a Histogram

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

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
 
Source

Difficulty:单调栈:栈内的元素,按照某种方式排序下(单调递增或者单调递减) 如果新入栈的元素破坏了单调性,就弹出栈内元素,直到满足单调性
 单调栈模板:

 int top=,s[maxn];
for(int i=;i<=n;i++)
{
while(top&&h[s[top]]>=h[i])//根据改变大于号小于号来改变单调性。
{
--top;//出栈,一般在while这重循环里添加东西去满足题意。
}
s[++top]=i; //入栈
}

单调栈解法:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long h[];
int s[];
long long area[];
int L[];
int R[];
int n;
long long ans;
bool cmp( int a, int c)
{
return a>c; }
int main()
{
while(~scanf("%d",&n)&&n)
{
int top=;
for(int i=;i<=n;i++)
{
scanf("%lld",&h[i]);
while(top&&h[s[top]]>=h[i])
--top;
L[i]=(top==?:s[top]+);
s[++top]=i;
}
top=;
for(int i=n;i>=;i--)
{
while(top&&h[s[top]]>=h[i])
--top;
R[i]=(top==?n:s[top]-);
s[++top]=i;
}
ans=-; for(int i=;i<=n;i++)
{
area[i]=h[i]*(R[i]-L[i]+);
if(ans<area[i])
ans=area[i]; }
printf("%lld\n",ans);
}
return ;
}

DP解法:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 251000+5
long long h[maxn];
int L[maxn];
int R[maxn];
int n,m;
int main()
{
while(~scanf("%d",&n))
{
if(n==)
break;
for(int i=;i<=n;i++)
{
scanf("%lld",&h[i]);
R[i]=L[i]=i;
}
h[]=-;
for(int i=;i<=n;i++)
{
while(h[i]<=h[L[i]-])
L[i]=L[L[i]-];
}
h[n+]=-;
for(int i=n;i>=;i--)
{
while(h[i]<=h[R[i]+])
R[i]=R[R[i]+];
}
long long max1=;
for(int i=;i<=n;i++)
{
max1=max(max1,(R[i]-L[i]+)*h[i]);
}
printf("%lld\n",max1);
} return ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏的更多相关文章

  1. Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏

    #include<iostream> #include<stack> #include<stdio.h> using namespace std; struct n ...

  2. 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 530人阅读 评论(0) 收藏

    在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...

  3. C/C++的四大内存分区 分类: C/C++ 2015-05-09 01:36 163人阅读 评论(0) 收藏

    导读 正确的理解C/C++程序的内存分区,是合格程序猿的基本要求. 网络上流形两大版本内存分区,分别为: 1. 五大内存分区:堆.栈.全局/静态存储区.自由存储区和常量存储区. 2. 五大内存分区:堆 ...

  4. Network Saboteur 分类: 搜索 POJ 2015-08-09 19:48 7人阅读 评论(0) 收藏

    Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10147 Accepted: 4849 Des ...

  5. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  6. Beautiful People 分类: Brush Mode 2014-10-01 14:33 100人阅读 评论(0) 收藏

    Beautiful People Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)   ...

  7. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  8. 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  9. Java 函数参数传递方式详解 分类: Java Game 2014-08-15 06:34 82人阅读 评论(0) 收藏

    转:http://zzproc.iteye.com/blog/1328591 在阅读本文之前,根据自己的经验和理解,大家可以先思考并选择一下Java函数的参数传递方式:  A. 是按值传递的?  B. ...

  10. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. jquery插件-省市联动

        由于项目需要需要实现一个省市联动,由于业务有一些特殊的需求,使用现有的插件略有不便,就自己实现了一个.     首先需要保存地区数据的JS数据文件,我这里命名为areaData.js,内容如下 ...

  2. Linux配置FTP服务器

    基于CentOS-6.5 1.先查看是否已安装:rpm -qa vsftpd 或 rpm -ql vsftpd 2.安装:yum install vsftpd -y 3.设置开机启动: chkconf ...

  3. C#~使用FileSystemWatcher来监视文件系统的变化

    对于一个文件夹的改变,C#这边有自己的类来实现,我们不需要关心它的内部实现机制,不需要关心它底层调用哪些API,我们只需要关心如何去调用它,如何让它帮助我们记录文件夹的修改情况即可. #region ...

  4. PHP连接Mysql服务器的操作

    我们的数据存储在数据库中以后,要把数据和网页联系起来的话,要通过web服务器的解释器进行读取数据,再传递给客户端网页.如图: 这里,我选择了PHP作为学习的解释器.下面就具体来总结一下PHP连接MYS ...

  5. java与.net比较学习系列(7) 属性

    文章摘自:http://www.cnblogs.com/mcgrady/p/3411405.html 说起属性,实际上java中没有属性这个概念,只有字段和方法,但是可以通过私有字段和声明get,se ...

  6. UISwitch + UIimage - 初识IOS

    这里解释一个小例子,希望对你有点帮助,利用UISwitch控制UIimage的动画效果 先定义一个数组,用来存放照片,现在定义数组有一个特别简单的方法: NSArray *image1 = @[]; ...

  7. jquery插件autocomplete

    项目中有时会用到自动补全查询,就像Google搜索框.淘宝商品搜索功能,输入汉字或字母,则以该汉字或字母开头的相关条目会显示出来供用户选择, autocomplete插件就是完成这样的功能. < ...

  8. jQuery中click()与trigger方法的区别

    click()可以执行单击事件,但是不可传参. $("button").click(function(){ alert("hello."); }); trigg ...

  9. Log4net 列

    配置 <configuration> <configSections> <section name="log4net" type="Syst ...

  10. (转)document.cookie.indexof的解释

    代码:function getCookie(c_name){ if(document.cookie.length > 0) { c_start = document.cookie.indexof ...