您的位置:首页 > 其它

hdu 1506:Largest Rectangle in a Histogram 【单调栈】

2017-03-05 16:36 399 查看
题目链接

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

1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long LL;
4
5 const int N=100010;
6 int Stack
;    //Stack[]为单调栈(即每次能入栈的元素值必比栈顶元素(若存在)要大)
7 int len
;        //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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: