HDU1506(单调栈或者DP) 分类: 数据结构 2015-07-07 23:23 2人阅读 评论(0) 收藏
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
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 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.
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.
4 1000 1000 1000 1000
0
4000
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) 收藏的更多相关文章
- Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20:48 81人阅读 评论(0) 收藏
#include<iostream> #include<stack> #include<stdio.h> using namespace std; struct n ...
- 百度地图-省市县联动加载地图 分类: Demo JavaScript 2015-04-26 13:08 530人阅读 评论(0) 收藏
在平常项目中,我们会遇到这样的业务场景: 客户希望把自己的门店绘制在百度地图上,通过省.市.区的选择,然后加载不同区域下的店铺位置. 先看看效果图吧: 实现思路: 第一步:整理行政区域表: 要实现通过 ...
- C/C++的四大内存分区 分类: C/C++ 2015-05-09 01:36 163人阅读 评论(0) 收藏
导读 正确的理解C/C++程序的内存分区,是合格程序猿的基本要求. 网络上流形两大版本内存分区,分别为: 1. 五大内存分区:堆.栈.全局/静态存储区.自由存储区和常量存储区. 2. 五大内存分区:堆 ...
- Network Saboteur 分类: 搜索 POJ 2015-08-09 19:48 7人阅读 评论(0) 收藏
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10147 Accepted: 4849 Des ...
- 多校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 ...
- 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) ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...
- 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- Java 函数参数传递方式详解 分类: Java Game 2014-08-15 06:34 82人阅读 评论(0) 收藏
转:http://zzproc.iteye.com/blog/1328591 在阅读本文之前,根据自己的经验和理解,大家可以先思考并选择一下Java函数的参数传递方式: A. 是按值传递的? B. ...
- 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 ...
随机推荐
- UGUI 快捷键创建UGUI组件
使用NGUI的时候还有xxx快捷键创建, spirte,label,button等等. 在UGUI里面的时候好像是没有快捷键的. 不知道以后多久才能有这个功能. 在家里闲无聊的时候写了一个脚本, ...
- Bom和Dom编程以及js中prototype的详解
一.Bom编程: 1.事件练习: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- OCP-1Z0-051-题目解析-第6题
6. Examine the structure of the SHIPMENTS table: name Null Type PO_ID ...
- android-sdk-windows版本号下载
Android SDK 4.0.3 开发环境配置及执行 近期又装了一次最新版本号的ADK环境 眼下最新版是Android SDK 4.0.3 本文的插图和文本尽管是Android2.2的 步骤都是一样 ...
- Zabbix使用外部邮箱服务器发送邮件报警
本来是想自己写一篇文章的,但是看到发现网上有写的不错的,于是乎又抄别人的文章,作为记录. 使用外部邮箱来发生邮件明显好处就是防止其他邮箱服务器当垃圾邮件处理,另一方面能降低收邮件延迟. 下面开始进行使 ...
- 关于自定义UICollectionViewLayout的一点个人理解<一>
自定义UICollectionView,主要会用到以下几个方法: - (void)prepareLayout; 第一次加载layout.刷新layout.以及- (BOOL)shouldInvalid ...
- .Net Memory -- GC基本知识
参考资料: http://blogs.msdn.com/b/tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of ...
- TaobaoProtect.exe,Alipaybsm.exe进程删除----让流氓软件滚粗
可能经常上网的朋友都会有这样的经历,只要你上过一次淘宝,那么阿里会给你的电脑自动下载一个TaobaoProtect.exe的程序,这是支付宝安全控件,名为安全控件,实际上它会在后台搜集用户数据和信息, ...
- Nubiers to follow
VGG Andrea Vedaldi Berkeley Trevor Darrell Jeff Donahue Ross Girshick Sergio Guadarrama Stanford And ...
- Install cv2.so for Anaconda
sudo apt-get install python-opencv cp /usr/lib/python2.7/dist-packages/cv2.so /opt/anaconda/lib/pyth ...