1. 程式人生 > >hdu 4699 Editor 多校第十場 (模擬)

hdu 4699 Editor 多校第十場 (模擬)

棧 L,R 維護游標之前(之後)的序列

那麼    I x        L.push(x);

            D       L.pop();

            L       R.push(L.pop());

           R       L.push(R.pop());

然後用  s[]陣列維護字首和,ans[]  陣列維護字首和的最大值

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1000010;
vector<int>L,R;
int s[maxn];
int ans[maxn];
int main()
{
    int T;
    while(~scanf("%d",&T))
    {
        L.clear();
        R.clear();
        char op[2];
        int tot=0;
        s[0]=0;

        while(T--)
        {
            scanf("%s",op);
            if(*op=='I')
            {
                int x;
                scanf("%d",&x);
                L.push_back(x);
                s[tot+1]=s[tot]+x;
                tot++;
                if(tot==1) ans[tot]=s[tot];
                else ans[tot]=max(s[tot],ans[tot-1]);
            }
            else if(*op=='D')
            {
                if(L.size()==0) continue;
                L.pop_back();
                tot--;
            }
            else if(*op=='L')
            {
                if(L.empty()) continue;
                R.push_back(L.back());
                L.pop_back();
                tot--;
            }
            else if(*op=='R')
            {
                if(R.empty()) continue;
                L.push_back(R.back());
                s[tot+1]=s[tot]+R.back();
                R.pop_back();
                tot++;
                ans[tot]=max(s[tot],ans[tot-1]);
            }
            else if(*op=='Q')
            {
                int k;
                scanf("%d",&k);
                printf("%d\n",ans[k]);
            }
        }
    }
}