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 }
相关文章推荐
- HDU 1506 && POJ 2559 Largest Rectangle in a Histogram (单调队列)
- !HDU 1506 Largest Rectangle in a Histogram-dp|单调队列-(dp降低时间复杂度)
- HDU 1506 Largest Rectangle in a Histogram 单调栈
- NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
- HDU - 1506 Largest Rectangle in a Histogram: 单调栈入门题
- HDU - 1506(单调队列) - Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram DP 单调队列
- hdu 1506 Largest Rectangle in a Histogram 单调栈
- hdu 1506 Largest Rectangle in a Histogram 单调队列
- HDU 1506 Largest Rectangle in a Histogram(单调队列)
- HDU 1506 Largest Rectangle in a Histogram 【动态规划】
- hdu 1506 Largest Rectangle in a Histogram
- poj 2559 hdu 1506 Largest Rectangle in a Histogram
- hdu 1506 Largest Rectangle in a Histogram
- HDU-1506 Largest Rectangle in a Histogram 动态规划
- hdu 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram
- HDU 1506 Largest Rectangle in a Histogram(单调栈)
- hdu 1506 Largest Rectangle in a Histogram
- HDU OJ 1506 Largest Rectangle in a Histogram 和 NYOJ 258 最大长方形(二) 【单调队列】