1 条题解

  • 1
    @ 2021-8-7 19:00:38

    C++ :

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    const int maxn = 1e6 + 7;
    stack<int>s1,s2;
    int f[maxn],sum[maxn];
    int main()
    {
        int Q;scanf("%d",&Q);
        sum[0] = 0;
        int cnt = 0;
        memset(f,-INF,sizeof(f));
        while(Q--)
        {
            char op[10];int n;
            scanf("%s",op);
            if(op[0] == 'I')
            {
                cnt++;
                scanf("%d",&n);
                sum[cnt] = sum[cnt - 1] + n;
                f[cnt] = max(f[cnt - 1],sum[cnt]);
                s1.push(n);
            }
            else if(op[0] == 'D')
            {
                if(!s1.empty())
                {
                    cnt--;
                    s1.pop();
                }
            }
            else if(op[0] == 'L')
            {
                if(!s1.empty())
                {
                    cnt--;
                    int tmp = s1.top();
                    s2.push(tmp);
                    s1.pop();
                }
            }
            else if(op[0] == 'R')
            {
                if(!s2.empty())
                {
                    cnt++;
                    int tmp = s2.top();
                    sum[cnt] = sum[cnt - 1] + tmp;
                    f[cnt] = max(f[cnt - 1],sum[cnt]);
                    s1.push(tmp);
                    s2.pop();
                }
            }
            else if(op[0] == 'Q')
            {
                scanf("%d",&n);
                printf("%d\n",f[n]);
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    39
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    287
    已通过
    84
    上传者