poj2796(单调栈)

http://poj.org/problem?id=2796

确定每个数边界,然后枚举

#include
#include
#include
using namespace std;
#define ll long long
const int maxn=1e5+10;
int L[maxn],R[maxn],a[maxn],n,l,r;
ll pre[maxn],ans=-1;
stacksta;

int main()
{
    scanf("%d",&n);
    a[0]=-1;
    a[n+1]=-1;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),pre[i]=pre[i-1]+a[i];
    for(int i=1;i<=n+1;i++)
    {
        if(sta.empty()||a[i]>=a[sta.top()])
            sta.push(i);
        else
        {
            while(!sta.empty()&&a[i]=0;i--)
    {
        if(sta.empty()||a[i]>=a[sta.top()])
            sta.push(i);
        else
        {
            while(!sta.empty()&&a[i]ans)
            ans=(pre[R[i]]-pre[L[i]-1])*a[i],l=L[i],r=R[i];
    }
    printf("%lld\n%d %d",ans,l,r);
    return 0;
}

 

你可能感兴趣的:(单调栈)