HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 1506

Appoint description:

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

本题的题意是给出若干个长方形,高度不一定,问你求得时在整个图形的范围内最大的面积是多少·

由于本题的高度很高,所以用int会出问题,

最好改成long long可以过了

下面给出代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100005;
typedef long long int ll;
ll l[maxn],r[maxn],h[maxn];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        if(!n)
        break;
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        memset(h,0,sizeof(h));
  //          memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
           scanf("%lld",&h[i]);

        }
        l[1]=1;
        r[n]=n;

        for(int i=2;i<=n;i++){
             int tmp=i;
            while(tmp>1&&h[i]<=h[tmp-1])
            tmp=l[tmp-1];

            l[i]=tmp;
        }

        for(int i=n-1;i>=1;i--){
              int tmp=i;
            while(tmp<n&&h[i]<=h[tmp+1])
            tmp=r[tmp+1];
            r[i]=tmp;
        }
        ll ans=-1;
        for(int i=1;i<=n;i++){
           ll tmp=(r[i]-l[i]+1)*h[i];
            ans=max(ans,tmp);
        }
      printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-03 13:45:22

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)的相关文章

hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))

# include <stdio.h> # include <algorithm> # include <iostream> # include <math.h> using namespace std; __int64 a[100010],l[100010],r[100010];///l[i]左边连续大于等于a[i]的下标,r[i]右边连续大于等于a[i]的下标,所以对于a[i]的矩形面积为(l[i]-r[i]+1)*a[i]; int main() {

HDU - 1506 Largest Rectangle in a Histogram(dp)

题意:已知n个高度不一.宽度相同的矩形并列排放,求所形成的图形中最大的矩形面积. 分析: 1.对于每一个矩形,分别算出它左边连续比它高的矩形中最左边的下标,右边同理.通过(r[i] - l[i] + 1) * a[i]比较得到最大的矩形面积. 2.将一组高度依次降低的矩形看成一个整体,如果该矩形比这个整体最矮的矩形都矮,长度可直接延伸过去,通过tmp = l[tmp - 1],依次向左比较,直到找到最左边的下标. #include<cstdio> #include<cstring>

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

hdu 1506 Largest Rectangle in a Histogram 构造

题目链接:HDU - 1506 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 rec

hdu 1506 Largest Rectangle in a Histogram 单调队列

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

HDU -1506 Largest Rectangle in a Histogram&amp;&amp;51nod 1158 全是1的最大子矩阵 (单调栈)

单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一个矩形的高度当作最后的高度,来从中选取最大值就可以了 但是如果它不是递增的,中间会出现低谷,那么要还想运用贪心策略就要把之前高度大于它的全部扔掉,但是再扔掉他们之前还要判断一下以他们为最后答案的高度可不可行,这样我们就是在构造一个递增序列,可以用栈来维护它 代码: 1 #include<stdio.

hdu 1506 Largest Rectangle in a Histogram(求最大的矩形)

1.注意要把a[]定义为LL,我在这里wa了N次 2.寻找边界时,得用dp思想 AC代码: #include<cstdio> #include<cstring> #define LL long long using namespace std; const LL INF=1<<60; LL a[100005];//要定义为LL int L[100005]; int R[100005]; int main() { int n; while(scanf("%d&q

hdu 1506 Largest Rectangle in a Histogram(DP)

题意: 有一个柱状图,有N条柱子.每一条柱子宽度都为1,长度为h1...hN. 在这N条柱子所构成的区域中找到一个最大面积,每平方米3块钱,问最多赚多少钱. 输入: 1<=N<=100000 0<=hi<=1000000000 思路: N很大,肯定得用一个O(N)或O(NLOGN)的算法,, 假如这个面积的长是从第i条柱子到第j条柱子,宽则一定是第i条柱子到第j条柱子中最矮的那条柱子的高度. 而我们站在那根最矮的柱子向左看,第i-1条的柱子一定是小于当前柱子的,否则可以加进去.向右

HDU 1506 Largest Rectangle in a Histogram【DP】

题意:坐标轴上有连续的n个底均为1,高为h[i]的矩形,求能够构成的最大矩形的面积. 学习的别人的代码 @[email protected] 看底的坐标怎么找的看了好一会儿--- 记l[i]为矩形的底的左边的坐标,就将它一直向左扩展 记r[i]为矩形的底的右边的坐标(倒着找,从n开始找,题解里面还着重强调了要倒着找,要不然就体现不出优化了)至于那个优化,我想的是,每一次算r[i]的时候,它前面的一个r[i]就是覆盖得最远的了,可以减少计算(用了一组样例试验) 然后就枚举找出面积的最大值-- #i