• AcWing 128 编辑器(对顶栈)


    题目链接

    解题思路

      对顶栈的模板题。L和R的操作可以由对顶栈来实现,至于查询操作用一个前缀和数组和一个存最大值的数组来存就行了。

    代码

    const int maxn = 1e6+10;
    stack<int> skl, skr;
    int pre[maxn], f[maxn] = {-114514}, k;
    int main() {
        int q; scanf("%d",&q);
        while(q--) {
            char ch[2]; int num; scanf("%s",ch);
            if (ch[0]=='I') {
                scanf("%d",&num);
                ++k;
                pre[k] = pre[k-1]+num;
                f[k] = max(f[k-1],pre[k]);
                skl.push(num);
            } 
            else if (ch[0]=='D') {
                if (!skl.empty()) {
                    --k; skl.pop();
                }
            }
            else if (ch[0]=='L') {
                if (!skl.empty()) {
                    --k;
                    skr.push(skl.top());
                    skl.pop();
                }
            }
            else if (ch[0]=='R') {
                if (!skr.empty()) {
                    ++k;
                    pre[k] = pre[k-1]+skr.top();
                    f[k] = max(f[k-1],pre[k]);
                    skl.push(skr.top());
                    skr.pop();
                }
            }
            else if (ch[0]=='Q') {
                scanf("%d",&num);
                printf("%d
    ",f[num]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Spring核心概念
    动态SQL
    SQL的映射文件
    初始MyBatis
    数据库SQL调优
    使用Spring Boot+MyBatis框架做查询操作
    SSM框架整合核心内容
    JavaScript基础
    MySQL的基本操作
    Java体系结构介绍
  • 原文地址:https://www.cnblogs.com/shuitiangong/p/13325419.html
Copyright © 2020-2023  润新知