HDU_1506_Largest Rectangle in a Histogram_dp
Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14177 Accepted Submission(s): 4049

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.
4 1000 1000 1000 1000
0
4000
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 100005 long long dpl[N],dpr[N],hei[N],maxn,t;
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
maxn=0;
for(int i=1;i<=n;i++)
scanf("%I64d",&hei[i]);
dpl[1]=1;
dpr[n]=n;
for(int i=2;i<=n;i++) //找当前矩形左边能延伸到的矩形,第几个,下标
{
t=i;
while(t>1&&hei[i]<=hei[t-1])
t=dpl[t-1];
dpl[i]=t;
}
for(int i=n-1;i;i--) //找当前矩形右边能够延伸到的矩形,第几个,下标
{
t=i;
while(t<n&&hei[i]<=hei[t+1])
t=dpr[t+1];
dpr[i]=t;
}
for(int i=1;i<=n;i++)
{
long long tot=(dpr[i]-dpl[i]+1)*hei[i];
if(tot>maxn)
maxn=tot;
}
cout<<maxn<<endl;
}
return 0;
}
HDU_1506_Largest Rectangle in a Histogram_dp的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Maximal Rectangle
很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- VMWare中的Host-only、NAT、Bridge的比較
VMWare有Host-only(主机模式).NAT(网络地址转换模式)和Bridged(桥接模式)三种工作模式. 1.bridged(桥接模式) 在这样的模式下.VMWare虚拟出来的操作系统就像是 ...
- Android ListView的item点击无响应的解决方法
假设listitem里面包含button或者checkbox等控件,默认情况下listitem会失去焦点,导致无法响应item的事件,最经常使用的解决的方法 是在listitem的布局文件里设置des ...
- Android开发之接收系统广播消息
BroadcastReceiver除了接收用户所发送的广播消息之外.另一个重要的用途:接收系统广播. 假设应用须要在系统特定时刻运行某些操作,就能够通过监听系统广播来实现.Android的大量系统事件 ...
- 蓝牙驱动分析 linux
蓝牙驱动分析 这个驱动分析的是OK6410开发板自带的内核版本是linux3.0.1,所支持的wifi和蓝牙一体芯片是marvell的8688和8787.根据开发板的设计,芯片与主机之间是通过sdio ...
- ReSharper warns: “Static field in generic type”
http://stackoverflow.com/questions/9647641/resharper-warns-static-field-in-generic-type It's fine to ...
- Django的CBV方式讲解
CBV使用配置 路径url的配置 cbv 顾名知义就是通过类的方法来调用,我们在url中配置为如下路径 url(r'^cbv.html/', views.Cbv.as_view()), 这里的Cbv是 ...
- 简述Python中的break和continue的区别
众所周知在Python中,break是结束整个循环体,而continue则是结束本次循环再继续循环. 但是作为一个新手的你,还是不明白它们的区别,这里用一个生动的例子说明它们的区别,如下: 1.con ...
- mysql机制总结
Innodb和myisam最大的不同就是 innodb支持事物 采用了行锁 myisam 采用了表锁 默认就使用了表锁 表锁:速度快 并发小 发生锁冲突高 开销小 行锁:速度慢 并发高 发生锁冲突低 ...
- webpack+vue-cli中proxyTable配置接口地址代理详细解释
在vue-cli项目中config目录里面的index.js配置接口地址代理,详细解释如下图所示:
- ACM_错排(递推dp)
RPG的错排 Time Limit: 2000/1000ms (Java/Others) Problem Description: 今年暑假GOJ集训队第一次组成女生队,其中有一队叫RPG,但做为集训 ...