hdu 1506 Largest Rectangle in a Histogram
2014-01-29 19:44
369 查看
Problem Description
A histogram is apolygon composed of a sequence of rectangles aligned at a common base line. Therectangles have equal widths but may have different heights. For example, thefigure on the
left shows the histogram that consists of rectangles with the heights2, 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., thefrequencies of characters in texts. Note that the order of the rectangles,i.e., their heights, is important. Calculate the area of the largest rectanglein
a histogram that is aligned at the common base line, too. The figure on theright shows the largest aligned rectangle for the depicted histogram.
Input
The input containsseveral test cases. Each test case describes a histogram and starts with aninteger n, denoting the number of rectangles it is composed of. You may assumethat 1 <= n <=
100000. Then follow n integers h1, ..., hn, where 0 <=hi <= 1000000000. These numbers denote the heights of the rectangles of thehistogram in left-to-right order. The width of each rectangle is 1. A zerofollows the input for the last test case.
Output
For each test caseoutput on a single line the area of the largest rectangle in the specifiedhistogram. Remember that this rectangle must be aligned at the common baseline.
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 10001000
0
Sample Output
8
4000
求最大的矩阵。先从左到右,算出自己的左边比自己高度小的位置。然后再从右往左,同样。然后用右边界-左边界乘上自己的高度,就是以该高度为宽的最大矩阵。这里有一个小小的优化,不用一个一个去找。以找左边边界为例。如果k=i,num[k-1]>=num[i],那么,就直接到k-1的左边界,即left[k-1],然后再去找。因为如果num[k-1]>=num[i],那么left[k-1]到k-1一定也在这个矩阵之中了。
A histogram is apolygon composed of a sequence of rectangles aligned at a common base line. Therectangles have equal widths but may have different heights. For example, thefigure on the
left shows the histogram that consists of rectangles with the heights2, 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., thefrequencies of characters in texts. Note that the order of the rectangles,i.e., their heights, is important. Calculate the area of the largest rectanglein
a histogram that is aligned at the common base line, too. The figure on theright shows the largest aligned rectangle for the depicted histogram.
Input
The input containsseveral test cases. Each test case describes a histogram and starts with aninteger n, denoting the number of rectangles it is composed of. You may assumethat 1 <= n <=
100000. Then follow n integers h1, ..., hn, where 0 <=hi <= 1000000000. These numbers denote the heights of the rectangles of thehistogram in left-to-right order. The width of each rectangle is 1. A zerofollows the input for the last test case.
Output
For each test caseoutput on a single line the area of the largest rectangle in the specifiedhistogram. Remember that this rectangle must be aligned at the common baseline.
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 10001000
0
Sample Output
8
4000
求最大的矩阵。先从左到右,算出自己的左边比自己高度小的位置。然后再从右往左,同样。然后用右边界-左边界乘上自己的高度,就是以该高度为宽的最大矩阵。这里有一个小小的优化,不用一个一个去找。以找左边边界为例。如果k=i,num[k-1]>=num[i],那么,就直接到k-1的左边界,即left[k-1],然后再去找。因为如果num[k-1]>=num[i],那么left[k-1]到k-1一定也在这个矩阵之中了。
k-1#include<cstdio> #include<cstring> #include<cstdlib> #define MAX 100010 #define LL long long using namespace std; LL num[MAX]; LL left[MAX] ,right[MAX]; LL ans; int main() { int n ,k; while(scanf("%d",&n),n) { ans = 0; for(int i = 0;i < n;i++) { scanf("%I64d",&num[i]); } for(int i = 0;i < n;i++) { k = i; while(k > 0 && num[k - 1] >= num[i]) { k = left[k - 1]; } left[i] = k; } for(int i = n-1;i >= 0;i--) { k = i; while(k < n - 1 && num[k + 1] >= num[i]) { k = right[k + 1]; } right[i] = k; if(ans < (right[i] - left[i] + 1) * num[i]) { ans = (right[i] - left[i] + 1) * num[i]; } } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- HDU 1505 Largest Rectangle in a Histogram && HDU 1506 City Game(动态规划)
- hdu 1506(nyoj 258)Largest Rectangle in a Histogram(最大长方形(二))(单调栈)
- hdu 1506-Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- HDU1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram DP 单调队列
- hdu 1506 Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram (poj 2559 && hdu 1506 矩形系列 迭代法)
- HDU 1506 Largest Rectangle in a Histogram
- HDU1506 Largest Rectangle in a Histogram (动规)
- HDU 1506 Largest Rectangle in a Histogram【DP】
- F - Largest Rectangle in a Histogram(HDU-1506)
- hdu 1506(nyoj 258)Largest Rectangle in a Histogram(最大长方形(二))(单调栈)
- HDU 1506 Largest Rectangle in a Histogram【矩阵最大面积】
- HDU 1506 Largest Rectangle in a Histogram(DP)
- Hdu 1506 Largest Rectangle in a Histogram
- HDU 1506(Largest Rectangle in a Histogram)动态规划
- HDU 1506 Largest Rectangle in a Histogram
- HDU-1506 Largest Rectangle in a Histogram(单调栈)