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:

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;
}

 

转载于:https://www.cnblogs.com/Asimple/p/6232720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值