hdu 1506 Largest Rectangle in a Histogram 单调栈

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100100],q[100100],l[100100],r[100100];
int main()
{
    int i,n,cnt;
    while(scanf("%d",&n),n!=0)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            l[i]=1;
            r[i]=n;
        }
        cnt=0;
        for(i=1;i<=n;i++)
        {
            while(cnt&&a[ q[cnt] ]>a[i])
            {
                r[q[cnt]]=i-1;
                cnt--;
            }
            q[++cnt]=i;
        }
        cnt=0;
        for(i=n;i>=1;i--)
        {
            while(cnt&&a[ q[cnt] ]>a[i])
            {
                l[q[cnt]]=i+1;
                cnt--;
            }
            q[++cnt]=i;
        }
        //for(i=1;i<=n;i++)
          //  printf("%d %d\n",l[i],r[i]);
        __int64 ans=0;
        for(i=1;i<=n;i++)
            ans=max(ans,(__int64)a[i]*(r[i]-l[i]+1));
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-10-22 23:52:20

hdu 1506 Largest Rectangle in a Histogram 单调栈的相关文章

HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的高度乘上左右边界的宽度求最大值就行了. 也可以用笛卡尔树上dp的方法搞一搞,即用每个结点权值和所在子树的大小分别表示高度和宽度.(建笛卡尔树的过程也用到了单调栈,作用是维护右链) 单调栈做法: 1 #include<bits/stdc++.h> 2 using namespace std; 3 t

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

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 (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 a

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

POJ 2559 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 rectang

POJ-2559 Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

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

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