HDU 4699 题解

题目大意

实现一个类似编辑器的东西,有五种操作
I x:在指针前插入一个数x
D:删除指针前的数
L:把指针往左移一个位置
R:把指针往右移一个位置
Q k:查询位置1~k的最大前缀和

我的做法

用两个栈维护指针左右两边的数
维护前缀和、最大前缀和
I操作等于在第一个栈插入一个元素
D操作等于弹出第一个栈顶的元素
L和R操作等于弹出一个栈顶的元素,再往另一个栈插入一个元素
Q操作直接输出答案

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t1[2000010];
int t2[2000010];
int a[2000010];
int s[2000010];
int top1,top2;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        a[0]=0x80000000;
        s[0]=0;
        top1=top2=0;
        int x;
        char c[100];
        int i;
        for(i=1;i<=n;i++)
        {
            scanf("%s",c);
            if(c[0]=='I')
            {
                scanf("%d",&x);
                t1[++top1]=x;
                s[top1]=s[top1-1]+x;
                a[top1]=max(a[top1-1],s[top1]);
            }
            else if(c[0]=='D')
            {
                top1--;
            }
            else if(c[0]=='L')
            {
                if(top1)
                 t2[++top2]=t1[top1--];
            }
            else if(c[0]=='R')
            {
                if(top2)
                {
                    t1[++top1]=t2[top2--];
                    s[top1]=s[top1-1]+t1[top1];
                    a[top1]=max(a[top1-1],s[top1]);
                }
            }
            else
            {
                scanf("%d",&x);
                printf("%d\n",a[x]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值