0x11 栈

这个不难吧,算是常识了。。毕竟也是刷过USACO的人

对顶栈这东西前几天才遇到过,好像和在线求中位数那东西放一起了吧

单调栈倒是没什么。。。贴个代码算了。一开始有点蠢的每个位置算,后来发现出栈再算就行了

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

int a[110000];
int top,sta[110000],w[110000];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        a[++n]=0;
        
        int top=0;LL ans=0;
        for(int i=1;i<=n;i++)
        {
            int L=0;
            while(top!=0&&sta[top]>a[i])
            {
                L+=w[top];
                ans=max(ans, (LL(L))*(LL(sta[top])) );
                top--;
            }
            sta[++top]=a[i];w[top]=L+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
poj2559

呜呜呜好困昨晚嗨皮今天早上3点才睡中午又去糜烂

猜你喜欢

转载自www.cnblogs.com/AKCqhzdy/p/9247312.html