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

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
 
2016.4.22再次做,一开始毫无思路,又看了题解。。。
思想:找出每一个单位宽度矩形的左边界(dpl)和右边界(dpr),左边界定义为左边连续的高度大于等于它的最左边的矩形的下标,右边界同理,从左往右推出所有的左边界,从右往左推出所有的右边界。
注意矩形的高度h可以等于零,以为这当左边边上的矩形高为零或者右边边界上矩形高度为零时,while循环无法停止,所以在while循环的条件中要加上限制边界的条件。
#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的更多相关文章

  1. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  5. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. Maximal Rectangle

    很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...

  8. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  9. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

随机推荐

  1. php删除数组中指定值的元素

    php删除数组中指定值的元素 /** * 删除数组中指定值的元素 * @author: ibrahim * @param array $arr 数组 * @param string $val 值 * ...

  2. 手游产品经理初探(二)从营销角度看loading界面

    近期開始写产品相关的专题,准备从细节入手去思考.总结一些不为人注意的细节地方. 今天给大家分享的是游戏里面都有的loading界面. 还是从几个在Facebook上排名靠前的Casino游戏的load ...

  3. Zabbix 监控服务器

    Zabbix 操作系统 :CentOS7.5 两台服务器: server端:192.168.206.6 client 端: 192.168.206.3 zabbix : 4.0 mariiadb : ...

  4. linux c 获取当前执行进程总数

    获取当前执行进程总数的命令为: ps auxw | wc -l 获取当前执行进程总数的源代码例如以下: #include <stdio.h> #include <stdlib.h&g ...

  5. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D "Or" Game 枚举+前缀后缀

                                                                            D. "Or" Game       ...

  6. 公钥加密,摘要算法MD5,SSH相关概念

    1.公钥加密,又叫非对称加密,一般指的是用一组密钥来保证通信的安全.(公钥,私钥)成对存在且惟一,典型的公钥算法有 RSA(计算出的是1024位,128字节),顺便提一下与公钥加密算法相对应的就是传统 ...

  7. clojure一些笔记

    https://xumingming.sinaapp.com/302/clojure-functional-programming-for-the-jvm-clojure-tutorial/#intr ...

  8. 利用JProfile 7分析内存OOM

    今天发现一个OOM问题,想来可能是有部分内存没有正确释放导致的 于是想到了之前使用的JProfile来分析一下 由于大概知道哪个类没有释放,可以在Memory Views里看一下对应的类有多少实例来确 ...

  9. 7-74 JavaScript 事件

    7-74 JavaScript 事件 学习要点 掌握常用的javaScript事件 基本概念 事件是一些特定动作发生时所发出的信号,JavaScript中的事件是可以被 JavaScript 侦测到的 ...

  10. web filter用spring注入对象

    tomcat容器初始化顺序监听器–>过滤器–>servlet,因此springMVCservlet初始化之前,过滤器就已经初始化过了,如果在过滤器中需要注入spring容器管理的bean是 ...