Editor HDU - 4699 (模拟)

4 篇文章 0 订阅
3 篇文章 0 订阅

这里写图片描述
Input
8
I 2
I -1
I 1
Q 3
L
D
R
Q 2
Output
2
3

思路:就是模拟了,关键是怎么模拟了,这里用两个栈,如果左移就做边出栈,然后压到右边的栈。通过两个栈来模拟光标左右移动,其也就差不多了。
代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<stack>>
#define maxx 30005
using namespace std;
stack<int> s1,s2;
int sum[1000005];
int ans[1000005];
int main()
{
    int n;

    while(scanf("%d",&n)==1)
    {
        while(s1.size())
            s1.pop();
        while(s2.size())
            s2.pop();
        char s[3];
        int x;
        while(n--)
        {
            scanf("%s",s);
            if(s[0]=='I')
            {
                scanf("%d",&x);
                s1.push(x);
                sum[s1.size()]=sum[s1.size()-1]+x;
                if(s1.size()==1)
                    ans[1]=sum[1];
                else
                    ans[s1.size()]=max(ans[s1.size()-1],sum[s1.size()]);
            }
            else
                if(s[0]=='Q')
                {
                    scanf("%d",&x);
                    printf("%d\n",ans[x]);
                }
                else
                    if(s[0]=='D')
                        s1.pop();
                    else
                        if(s[0]=='L')
                        {
                            if(s1.size())
                            {
                                int x=s1.top();
                                s1.pop();
                                s2.push(x);
                            }
                        }
                        else
                        {
                            if(s2.size())
                            {
                                int x=s2.top();
                                s2.pop();
                                s1.push(x);
                                sum[s1.size()]=sum[s1.size()-1]+x;
                                if(s1.size()==1)
                                    ans[1]=sum[1];
                                else
                                    ans[s1.size()]=max(ans[s1.size()-1],sum[s1.size()]);
                            }
                        }

        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值