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 ...
随机推荐
- php删除数组中指定值的元素
php删除数组中指定值的元素 /** * 删除数组中指定值的元素 * @author: ibrahim * @param array $arr 数组 * @param string $val 值 * ...
- 手游产品经理初探(二)从营销角度看loading界面
近期開始写产品相关的专题,准备从细节入手去思考.总结一些不为人注意的细节地方. 今天给大家分享的是游戏里面都有的loading界面. 还是从几个在Facebook上排名靠前的Casino游戏的load ...
- Zabbix 监控服务器
Zabbix 操作系统 :CentOS7.5 两台服务器: server端:192.168.206.6 client 端: 192.168.206.3 zabbix : 4.0 mariiadb : ...
- linux c 获取当前执行进程总数
获取当前执行进程总数的命令为: ps auxw | wc -l 获取当前执行进程总数的源代码例如以下: #include <stdio.h> #include <stdlib.h&g ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D "Or" Game 枚举+前缀后缀
D. "Or" Game ...
- 公钥加密,摘要算法MD5,SSH相关概念
1.公钥加密,又叫非对称加密,一般指的是用一组密钥来保证通信的安全.(公钥,私钥)成对存在且惟一,典型的公钥算法有 RSA(计算出的是1024位,128字节),顺便提一下与公钥加密算法相对应的就是传统 ...
- clojure一些笔记
https://xumingming.sinaapp.com/302/clojure-functional-programming-for-the-jvm-clojure-tutorial/#intr ...
- 利用JProfile 7分析内存OOM
今天发现一个OOM问题,想来可能是有部分内存没有正确释放导致的 于是想到了之前使用的JProfile来分析一下 由于大概知道哪个类没有释放,可以在Memory Views里看一下对应的类有多少实例来确 ...
- 7-74 JavaScript 事件
7-74 JavaScript 事件 学习要点 掌握常用的javaScript事件 基本概念 事件是一些特定动作发生时所发出的信号,JavaScript中的事件是可以被 JavaScript 侦测到的 ...
- web filter用spring注入对象
tomcat容器初始化顺序监听器–>过滤器–>servlet,因此springMVCservlet初始化之前,过滤器就已经初始化过了,如果在过滤器中需要注入spring容器管理的bean是 ...