单调栈

傻逼zzy...

poj2796

注意一下最后答案可能是0,所以res的初始值要赋为-1

#include <iostream>
#include <cstdio>
using namespace std;
int n,top;
long long res;
int ans;
long long s[100005], a[100005];
int L[100005], R[100005];
struct node
{
    long long x;
    int pos;
    node(long long a = 0, int b = 0):x(a), pos(b){}
}st[100005];
int main()
{
    scanf("%d", &n);
    s[0] = 0;
    for (int i=1;i<=n;i++)
    {
        scanf("%lld", &a[i]);
        s[i] = s[i-1] + a[i];
        L[i] = 0;R[i] = n+1;
    }
    top = 0;
    for (int i=1;i<=n;i++)
    {
        while (top > 0 && st[top].x > a[i])
        {
            R[st[top].pos] = i;
            top--;
        }
        st[++top] = node(a[i], i);
    }
    top = 0;
    for (int i=n;i>=1;i--)
    {
        while (top > 0 && st[top].x > a[i])
        {
            L[st[top].pos] = i;
            top--;
        }
        st[++top] = node(a[i], i);
    }
    ans = 0;
    res = -1;
    for (int i=1;i<=n;i++)
    {
        if (res < a[i] * (s[R[i]-1] - s[L[i]]))
        {
            res = a[i] * (s[R[i]-1] - s[L[i]]);
            ans = i;
        }
    }
    printf("%lld\n", res);
    printf("%d %d\n", L[ans]+1, R[ans]-1);
}

poj2559

//最大矩形面积
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
#define N 100005
LL ans,a[N];
int n,top;
int L[N], R[N];
struct node
{
    LL x;
    int pos;
    node (LL a = 0, int b = 0):x(a), pos(b){}
}st[N];
int main()
{
    while (scanf("%d", &n) && n)
    {
        for (int i=1;i<=n;i++)
            scanf("%lld", &a[i]);
        top = 0;
        for (int i=1;i<=n;i++)
        {
            while (top > 0 && st[top].x > a[i])
            {
                R[st[top].pos] = i;
                top--;
            }
            st[++top] = node(a[i], i);
        }
        while (top > 0)
            R[st[top].pos] = n+1, top--;
        top = 0;
        for (int i=n;i>=1;i--)
        {
            while (top > 0 && st[top].x > a[i])
            {
                L[st[top].pos] = i;
                top--;
            }
            st[++top] = node(a[i], i);
        }
        while (top > 0)
            L[st[top].pos] = 0, top--;
        ans = 0;
        for (int i=1;i<=n;i++)
            ans = max(ans, a[i] * (R[i] - L[i] - 1));
        printf("%lld\n", ans);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值