hdu_1506:Largest Rectangle in a Histogram 【单调栈】

题目链接

对栈的一种灵活运用吧算是,希望我的注释写的足够清晰。。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4
 5 const int N=100010;
 6 int Stack[N];    //Stack[]为单调栈(即每次能入栈的元素值必比栈顶元素(若存在)要大)
 7 int len[N];        //len[]与Stack[]同步 ,记录的是从Stack[]中对应元素起向左最大可延伸的宽度
 8 int n;
 9 LL top,ans,h;
10
11 void print()
12 {
13     printf("top=%d,    ans=%d\n",top,ans);
14     printf("\tStack:");
15     for(int i=0;i<=top;i++)
16         printf("%6d",Stack[i]);
17     puts("");
18     printf("\t  len:");
19     for(int i=0;i<=top;i++)
20         printf("%6d",len[i]);
21     puts("");
22 }
23
24 int main()
25 {
26     while(scanf("%d",&n)&&n)
27     {
28         memset(Stack,0,sizeof(Stack));
29         memset(len,0,sizeof(len));
30         top=-1,ans=0;
31         for(int i=0; i<=n; i++)
32         {
33             if(i<n) scanf("%lld",&h);
34             else h=-1;    //用作结束标记
35             if(top<0||Stack[top]<h)
36             {
37                 Stack[++top]=h;    //    h入栈
38                 len[top]=1;        //    显然新入栈元素比原栈顶元素大,此时向左最大延伸“1”宽度
39             }                    //  “1”可根据具体题目进行修改~
40             else    // if(top>=0&&Stack[top]>=h
41             {
42                 int l=0;
43                 while(Stack[top]>=h&&top>=0)
44                 {
45                     ans=max(ans,(LL)(len[top]+l)*Stack[top]);    //更新ans
46                     l+=len[top--];    //Stack[]和len[]同时将栈顶元素弹出
47                 }    //循环结束后, top<0||Stack[top]<h
48                 if(h>0)
49                 {
50                     Stack[++top]=h;    //    h入栈
51                     len[top]=l+1;    //    以top为起点的最长连续(h>=Stack[top])的区间长度
52                 }
53             }
54 //            print();
55         }
56         printf("%lld\n",ans);
57     }
58 }
时间: 2025-01-03 13:37:15

hdu_1506:Largest Rectangle in a Histogram 【单调栈】的相关文章

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 (单调栈/笛卡尔树)

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

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",

hdu1506---Largest Rectangle in a Histogram(单调栈)

Problem 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

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

POJ2559 Largest Rectangle in a Histogram 单调队列

题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于等于另一个小矩形的高. 我们现考虑面积最大矩形左边高等于其所在小矩形的高的情况,则其右边高小于等于其对应的小矩形的高(以后将此简称为左等高矩形).我们要维护一个单调栈,使得栈里的矩形的高度都是单调递增的.一个一个枚举小矩形,如果当前小矩形高度比栈顶的矩形高度高或等,则如果所求大矩形是个左边为栈内矩形

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

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