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 ...
随机推荐
- Android GMS无法通过网络定位
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载.但请保留文章原始出处: CSDN:http://www.csdn.net ...
- openwrt-安装-驱动-应用-lcd2004a实验
1. 板子f403tech的RT5350的板子和 (1)openWRT系统的定义和特点 OpenWrt是一个高度模块化.高度自己主动化的嵌入式Linux系统.拥有强大的网络组件.经常被 ...
- 在64位的ubuntu 14.04 上开展32位Qt 程序开发环境配置(pro文件中增加 QMAKE_CXXFLAGS += -m32 命令)
为了能中一个系统上开发64或32位C++程序,费了些周折,现在终于能够开始干过了.在此记录此时针对Q5.4版本的32位开发环境配置过程. 1. 下载Qt 5.4 的32位版本,进行安装,安装过程中会发 ...
- MATLAB——matlab特殊符号表【转载】
链接来源: matlab特殊符号表 http://blog.sina.com.cn/s/blog_4a09187801014xg9.html Character Sequence Symbol Cha ...
- NOI.AC #31 MST —— Kruskal+点集DP
题目:http://noi.ac/problem/31 好题啊! 题意很明白,对于有关最小生成树(MST)的题,一般是要模拟 Kruskal 过程了: 模拟 Kruskal,也就是把给出的 n-1 条 ...
- POJ2503 Babelfish map或者hash_map
POJ2503 这是一道水题,用Map轻松AC. 不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列. 自己生成一个质数, 随便搞一下. #include&l ...
- mipi差分信号原理
差分信号,什么是差分信号 一个差分信号是用一个数值来表示两个物理量之间的差异.从严格意义上来讲,所有电压信号都是差分的,因为一个电压只能是相对于另一个电压而言的.在某些系统里,系统’地’被用作电压基准 ...
- WP8 中使用HTML Agility Pack与友盟分享SDK遇到的 System.Xml.XPath加载问题
今晚在尝试使用友盟最新的社交分享SDK时,按照官方Demo,并未做多少多少改动,就是去除了对微信.脸书和推特的分享.然后运行之后就一直报错 : {System.IO.FileLoadException ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路【最小生成树】
先把已有的边并查集了,然后MST即可 记得开double #include<iostream> #include<cstdio> #include<algorithm&g ...
- linux下jdk环境变量配置深度分析----解决环境变量不生效的问题
1.linux下jdk环境变量配置 是否需要配置环境变量,主要看java -version 显示的版本是否为你期望的版本 1.1 不需要配置环境变量的情况 使用java -version查看,版本显示 ...