hdoj-1506-Largest Rectangle in a Histogram【动态规划】

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 14050 Accepted Submission(s): 4008

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

Source

University of Ulm Local Contest 2003

Recommend

LL | We have carefully selected several similar problems for you:1058
2870 2830 2159 1421

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
__int64 a[100010];
__int64 l[100010],r[100010];
int main(){
    int n;
	while(scanf("%d",&n),n)	{
		for(int i=1;i<=n;++i){
			scanf("%I64d",&a[i]);
		}
		a[0]=a[n+1]=-1;l[0]=0,r[n+1]=0;
		for(int i=1;i<=n;++i){
			l[i]=i;
			while(a[l[i]-1]>=a[i]){
				l[i]=l[l[i]-1];
			}
		}
		for(int i=n;i>=1;--i){
			r[i]=i;
			while(a[r[i]+1]>=a[i]){
				r[i]=r[r[i]+1];
			}
		}
		__int64 maxn=0;
		for(int i=1;i<=n;++i)
		  maxn=max(maxn,(r[i]-l[i]+1)*a[i]);
		printf("%I64d\n",maxn);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 06:54:37

hdoj-1506-Largest Rectangle in a Histogram【动态规划】的相关文章

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506 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 rec

hdu 1506 Largest Rectangle in a Histogram 单调队列

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

hdu 1507 Largest Rectangle in a Histogram 动态规划计算最大面积

记录动态规划dpl,dpr,分辨记录i左面的比i大的,右面比i大的,然后(dpr[i]-dpl[i]+1)*h[i]得出长度 动态转移方程while(temp>1 && h[temp-1]>=h[i]) temp=dpl[temp-1] /************************************************************************* > File Name: hdu1506.cpp > Author: yang &

HDU -1506 Largest Rectangle in a Histogram&amp;&amp;51nod 1158 全是1的最大子矩阵 (单调栈)

单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一个矩形的高度当作最后的高度,来从中选取最大值就可以了 但是如果它不是递增的,中间会出现低谷,那么要还想运用贪心策略就要把之前高度大于它的全部扔掉,但是再扔掉他们之前还要判断一下以他们为最后答案的高度可不可行,这样我们就是在构造一个递增序列,可以用栈来维护它 代码: 1 #include<stdio.

hdu 1506 Largest Rectangle in a Histogram 单调栈

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int a[100100],q[100100],l[100100],r[100100]; int main() { int i,n,cnt; while(scanf("%d",&n),n!=0) { for(i=1;i<=n;i++) { scanf("%d",

hdu 1506 Largest Rectangle in a Histogram(DP)

题意: 有一个柱状图,有N条柱子.每一条柱子宽度都为1,长度为h1...hN. 在这N条柱子所构成的区域中找到一个最大面积,每平方米3块钱,问最多赚多少钱. 输入: 1<=N<=100000 0<=hi<=1000000000 思路: N很大,肯定得用一个O(N)或O(NLOGN)的算法,, 假如这个面积的长是从第i条柱子到第j条柱子,宽则一定是第i条柱子到第j条柱子中最矮的那条柱子的高度. 而我们站在那根最矮的柱子向左看,第i-1条的柱子一定是小于当前柱子的,否则可以加进去.向右

hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))

# include <stdio.h> # include <algorithm> # include <iostream> # include <math.h> using namespace std; __int64 a[100010],l[100010],r[100010];///l[i]左边连续大于等于a[i]的下标,r[i]右边连续大于等于a[i]的下标,所以对于a[i]的矩形面积为(l[i]-r[i]+1)*a[i]; int main() {

HDU - 1506 Largest Rectangle in a Histogram(dp)

题意:已知n个高度不一.宽度相同的矩形并列排放,求所形成的图形中最大的矩形面积. 分析: 1.对于每一个矩形,分别算出它左边连续比它高的矩形中最左边的下标,右边同理.通过(r[i] - l[i] + 1) * a[i]比较得到最大的矩形面积. 2.将一组高度依次降低的矩形看成一个整体,如果该矩形比这个整体最矮的矩形都矮,长度可直接延伸过去,通过tmp = l[tmp - 1],依次向左比较,直到找到最左边的下标. #include<cstdio> #include<cstring>