整数序列编辑

Author Avatar
Axell 1月 13, 2019
  • 在其它设备中阅读本文章

题目描述

题面

思路

可以建立2个栈,一个存放光标后面的数,一个存放光标前面的数,不断维护答案即可

代码

#include <bits/stdc++.h>
using namespace std;

int f[1000005],ans[1000005],top,tmp,t,cnt,now,step;
char s[10];

stack <int> a;
stack <int> b;

int main(){
    cin>>t;
    for (int i=0;i<=t;++i) ans[i]=-1e9;
    while (t--){
        scanf("%s",s);
        if (s[0]=='I'){
            scanf("%d",&tmp);
            a.push(tmp);
            int top=a.size();
            f[top]=f[top-1]+tmp;
            ans[top]=max(ans[top-1],f[top]);
        }else if(s[0]=='D'){
            a.pop();
        }else if(s[0]=='L'){
            if (!a.empty()){
                int tt=a.top();
                a.pop();
                b.push(tt);
            }
        }else if(s[0]=='R'){
            if (!b.empty()){
                int tt=b.top();
                b.pop();
                a.push(tt);
                int top=a.size();
                f[top]=f[top-1]+tt;
                ans[top]=max(ans[top-1],f[top]);
            }
        }else{
            scanf("%d",&tmp);
            printf("%d\n",ans[tmp]);
        }
    }
    // a.push(1);
    return 0;
}

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可。

本文链接:https://hs-blog.axell.top/archives/69/