Largest Rectangle in a Histogram POJ - 2559
作者:互联网
很显然是单调栈
这里记录一种新的写法,这种写法基于递推,但是相比之下比单调栈更好写
#include<cstdio> #include<map> #include<set> #include<queue> #include<string> #include<stack> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using namespace std; //#define endl "\n" #define inf 0x3f3f3f3f #define me(a,b) memset(a,b,sizeof(a)) #define maxn 100000+5 long long n,a[maxn]; int l[maxn],r[maxn]; stack<long long> st; long long mx; void init() { mx=0; while(!st.empty()) st.pop(); } int main() { while(cin>>n&&n){ init(); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]),l[i]=r[i]=i; a[0]=0; for(int i=2;i<=n;i++){ int now=i; while(now>1&&a[i]<=a[now-1]) now=l[now-1]; l[i]=now; } for(int i=n-1;i;i--){ int now=i; while(now<n&&a[i]<=a[now+1]) now=r[now+1]; r[i]=now; } for(int i=1;i<=n;i++) mx=max(mx,a[i]*(r[i]-l[i]+1)); cout<<mx<<endl; } }
标签:int,2559,long,Histogram,maxn,st,POJ,include,define 来源: https://www.cnblogs.com/033000-/p/10466548.html