首页 > 代码库 > 单调栈

单调栈

#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;

stack<long long> s;
long long a[100005];
int main()
{
    int n;
    while(scanf("%d",&n) && n)
    {
        while(!s.empty())   s.pop();
        long long ans = 0;
        for(int i = 1;i <= n;i++)   scanf("%lld",&a[i]);
        a[n+1] = 0;
        for(int i = 1;i <= n+1;i++)
        {
            while(!s.empty() && a[s.top()] > a[i])
            {
                int temp = s.top();
                s.pop();
                int len = s.empty()?i-1:i-s.top()-1;
                ans = max(ans,len*a[temp]);
            }
            s.push(i);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

//HDU1506

 

单调栈