AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

AcWing 128. 编辑器    原题链接    中等

作者: 作者的头像   Object_ ,  2019-10-26 10:09:31 ,  所有人可见 ,  阅读 1001


0


注意点:

  • 最小前缀和仅需要维护光标前的数值,因此sumi及fi可直接依托top_1(代表光标前数值的栈)存在.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=2e6,INF=2e9;
int stck_1[MAXN],top_1=0;//光标前
int f[MAXN];//最大前缀和
int id=0;
int sum[MAXN];//前缀和
int stck_2[MAXN],top_2=0;//光标后
int main(){
    memset(f,0xcf,sizeof(f));
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++){
        char opt;
        cin>>opt;
        if(opt=='I'){//光标前插入数值
            int x;
            scanf("%d",&x);
            stck_1[++top_1]=x;
            sum[top_1]=sum[top_1-1]+x;
            f[top_1]=max(f[top_1-1],sum[top_1]);
        }else if(opt=='Q'){//查询最大前缀和
            int x;
            scanf("%d",&x);
            printf("%d\n",f[x]);
        }else if(opt=='L'){//光标左移
            if(!top_1)continue;
            stck_2[++top_2]=stck_1[top_1--];
        }else if(opt=='R'){//光标右移
            if(!top_2)continue;
            stck_1[++top_1]=stck_2[top_2--];
            sum[top_1]=sum[top_1-1]+stck_1[top_1];
            f[top_1]=max(f[top_1-1],sum[top_1]);
        }else if(opt=='D'){//删除光标前一个元素
            if(!top_1)continue;
            top_1--;
        }
    }
    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息