hdu1506(单调栈)

传送门

ac代码:

#include<bits/stdc++.h>
#define per(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
//#define int long long
const ll inf =2333333333333333LL;
const double eps=1e-8;
int read(){
    char ch=getchar();
    int res=0,f=0;
    while(ch<'0' || ch>'9'){f=(ch=='-'?-1:1);ch=getchar();}
    while(ch>='0'&&ch<='9'){res=res*10+(ch-'0');ch=getchar();}
    return res*f;
}
// ------------------------head
#define mod 1000000007
const int N=100005;
int n;
stack<int>st;
int h[N];
int L[N],R[N];

signed main()
{
    while(scanf("%d",&n)!=EOF&&n!=0){
        while(!st.empty())st.pop();
        per(i,1,n)scanf("%lld",&h[i]);
        per(i,1,n){
            while(!st.empty()&&h[i]<=h[st.top()])st.pop();
            if(st.empty())L[i]=1;
            else L[i]=st.top()+1;
            st.push(i);
        }
        while(!st.empty())st.pop();
        for(int i=n;i>0;i--){
            while(!st.empty()&&h[i]<=h[st.top()])st.pop();
            if(st.empty())R[i]=n+1;
            else R[i]=st.top();
            st.push(i);
        }
        ll ans=0;
        //per(i,1,n)printf("i:%d  L:%d R:%d\n",i,L[i],R[i]);//
        per(i,1,n){
            ans=max(ans,(ll)h[i]*(R[i]-L[i]));
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2018-09-17 12:02  WindFreedom  阅读(354)  评论(0)    收藏  举报