hdu 1506#单调队列
2012-07-19 10:16
169 查看
Largest Rectangle in a Histogram
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4975 Accepted Submission(s): 1436
[/b]
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
#include <stdio.h> const int N = 100001; int a ; int pre ,nxt ;//pre[i]表示连续大于a[i]的左边界 int n,i; int main() { while(scanf("%d",&n) == 1 && n) { for(i = 0; i < n; ++i) { scanf("%d",a + i); pre[i] = nxt[i] = i; } for(i = 1; i < n; ++i) while(pre[i] > 0 && a[pre[i]-1] >= a[i]) pre[i] = pre[pre[i]-1]; for(i = n - 2; i >= 0; --i) while(nxt[i] < n - 1 && a[nxt[i]+1] >= a[i]) nxt[i] = nxt[nxt[i]+1]; __int64 tmp,res = 0; for(i = 0; i < n; ++i) { tmp = (__int64)a[i] * (nxt[i] - pre[i] + 1LL); if(tmp > res) res = tmp; } printf("%I64d\n",res); } return 0; } /* 10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 */
相关文章推荐
- HDU 4123 Bob’s Race 树形dp+单调队列
- hdu 4123 Bob’s Race(单调队列或者rmq)
- hdu 4123 树形DP+单调队列
- hdu 3415 Max Sum of Max-K-sub-sequence (单调队列)
- hdu 1506 单调队列
- HDU 4258 Covered Walkway【单调队列斜率优化】
- HDU-3401 Trade 单调队列优化DP
- !HDU 4193 循环序列前缀和非负-单调队列-(区间最值)
- hdu 4374 (单调队列+dp)
- 单调队列-hdu-4193-Non-negative Partial Sums
- hdu 5945 Fxx and Game dp(单调队列优化)
- 【hdu 5945 】 【dp+单调队列优化】Fxx and game【求数x最少经过多少次变换能变为1,(1)如果x%k==0,那么可以x=x/k。(2)x=x-i,(1<=i<=t)】
- hdu 5380 Travel with candy 单调队列 2015 Multi-University Training Contest 8
- HDU 6047 Maximum Sequence (贪心+单调队列)
- hdu 3474 Necklace(单调队列)
- hdu 5945 Fxx and game(单调队列,dp)
- 单调队列1001 HDU 3706 Second My Problem First 单调队列入门题
- HDU 3415 Max Sum of Max-K-sub-sequence(长度不超过k的最大连续子序列和,单调队列)
- HDU 1193 Non-negative Partial Sums / 单调队列
- HDU 2993 MAX Average Problem(斜率优化入门+单调队列)