Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3722 Accepted Submission(s): 1104
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
题目大意:给你一块柱状图,要求放一块矩形进去,使得矩形最大,求这个矩形的面积。用DP解决。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
int l[100005], r[100005];
long long h[100005];
int main()
{
int i, n;
long long area, MAX; //数据很大,用long long防止溢出
while(scanf("%d", &n), n)
{
for(i = 1; i <= n; i++)
{
scanf("%I64d", &h[i]);
l[i] = r[i] = i;
}
h[0] = h[n+1] = -1; //-1防止死循环,高度可能为0
for(i = n; i >= 1; i--) //找右界,要从最右边开始找,不然TLE
{
while(h[i] <= h[ r[i]+1 ])
{
r[i] = r[ r[i]+1 ];
}
}
for(i = 1; i <= n; i++) //找左界,要从最左边开始找
{
while(h[i] <= h[ l[i]-1 ])
{
l[i] = l[ l[i]-1 ];
}
}
MAX = 0;
for(i = 1; i <= n; i++)
{
area = (r[i] - l[i] + 1) * h[i]; //面积:该点的(右界-左界+1)*高度
MAX = max(MAX, area);
}
printf("%I64d\n", MAX);
}
return 0;
}
- 大小: 2.4 KB
分享到:
相关推荐
HDU的一题........HDU DP动态规
Largest prime factor Everybody knows any number can be combined by the prime number. Now, your task is telling me what position of the largest prime factor. The position of prime 2 is 1, prime 3 is 2,...
动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,...
HDU上DP大集合,里面包括题,题解,代码,对DP入门者很实用,对DP老手也是有很大的提高
动态规划DP题解 POJ HDU部分动态规划DP题解
acm hdu as easy as a+b
hdu 1005.比较简单的一道题,有兴趣的可以看看。
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu_2102_passed_sorce
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧