AcWing 128. 编辑器(对顶栈)

题目链接:点击这里

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <limits.h>

using namespace std;

const int N = 1000010;

int stkl[N], stkr[N], topl, topr;
int f[N], sum[N];

void push_left(int x)
{
    stkl[ ++ topl] = x;
    sum[topl] = sum[topl - 1] + x;
    f[topl] = max(f[topl - 1], sum[topl]);
}

int main()
{
    int n;
    scanf("%d", &n);
    char ops[2];
    f[0] = INT_MIN;     //max要求f[0]初值为INT_MIN
    while (n -- )
    {
        int x;
        scanf("%s", ops);
        if (*ops == 'I')
        {
            scanf("%d", &x);
            push_left(x);
        }
        else if (*ops == 'D')
        {
            if (topl) topl -- ;
        }
        else if (*ops == 'L')
        {
            if (topl) stkr[ ++ topr] = stkl[topl -- ];
        }
        else if (*ops == 'R')
        {
            if (topr) push_left(stkr[topr -- ]);
        }
        else
        {
            scanf("%d", &x);
            printf("%d\n", f[x]);
        }
    }
    return 0;
}
发布了727 篇原创文章 · 获赞 111 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104293698