DP专题训练之HDU 1506 Largest Rectangle in a Histogram
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:
![](http://acm.hdu.edu.cn/data/images/1506-1.gif)
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.
![](http://acm.hdu.edu.cn/data/images/1506-1.gif)
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
看别人的题解才知道大概的思路。还是要多做题,多想题~~
题目意思很简单,就算看不懂题目,看图+样例也可以了解。
大意就是:给你一串数字,代表着几个互相连接的矩形,每个数字代表着一个矩形的高,让你求可以得到的最大的矩形面积。(结合题目的图更易看懂~)
看上去我们似乎又要求长,又要求高,似乎很难~~(当时我就是这么想的~~)
但其实,我们可以其中的一个点为中心,然后找左边不大于中心点的矩形的个数,再找右边不大于中心点的矩形的个数,两边个数相加就是矩形的长,而这个中心点的高度就是我们求的矩形的高度~~具体代码如下:
//Asimple #include <iostream> #include <sstream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <cctype> #include <cstdlib> #include <stack> #include <cmath> #include <set> #include <map> #include <string> #include <queue> #include <limits.h> #include <time.h> #define INF 0xfffffff #define mod 1000000 #define swap(a,b,t) t = a, a = b, b = t #define CLS(a, v) memset(a, v, sizeof(a)) #define debug(a) cout << #a << " = " << a <<endl #define abs(x) x<0?-x:x #define srd(a) scanf("%d", &a) #define src(a) scanf("%c", &a) #define srs(a) scanf("%s", a) #define srdd(a,b) scanf("%d %d",&a, &b) #define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c) #define prd(a) printf("%d\n", a) #define prdd(a,b) printf("%d %d\n",a, b) #define prs(a) printf("%s\n", a) #define prc(a) printf("%c", a) using namespace std; typedef long long ll; const int maxn = 100001; ll n, m, num, T, k; ll a[maxn], l[maxn], r[maxn]; void input() { while( ~scanf("%lld", &n) && n ) { for(int i=1; i<=n; i++) { scanf("%lld", &a[i]); } l[1] = 1; r[n] = n; //左边不小于a[i]的数 for(int i=2; i<=n; i++) { k = i; while( k > 1 && a[i]<=a[k-1] ) { k = l[k-1]; } l[i] = k; } //右边不小于a[i]的数 for(int i=n-1; i>=1; i--) { k = i; while( k<n && a[i]<=a[k+1]) { k = r[k+1]; } r[i] = k; } ll Max = 0; for(int i=1; i<=n; i++) { if( (r[i]-l[i]+1)*a[i] > Max ) { Max = (r[i]-l[i]+1)*a[i]; } } printf("%lld\n", Max); } } int main(){ input(); return 0; }
低调做人,高调做事。