[HDOJ4699]Editor

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/zyszlb2003/article/details/98300412

题面描述

传送门

思路

大概就是两个栈不断出栈入栈,一个主栈,一个辅助栈,

辅助栈用来存储当前光标以后的所有值。

AC code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const int N=1e6+10;
const ll inf=1e18;
/*用两个栈来存储结果,模拟*/
struct my_stack
{
    int p,a[N];ll f[N],sum[N];
    void clear(){sum[0]=0;f[0]=-inf;p=0;}
    void ins(int x)
    {
        a[++p]=x;
        sum[p]=sum[p-1]+a[p];//前缀和 
        f[p]=max(f[p-1],sum[p]);//统计[1,p]中的最大前缀和 
    }
    void change(){sum[p]=f[p]=a[p]=0;}
    int top(){return a[p];}
    void pop(){p--;}
    ll query(int x){return f[x];}
}q1;
int b[N],pb;
int main()
{
	freopen("1973.in","r",stdin);
	freopen("1973.out","w",stdout);
    int t;scanf("%d",&t);
    q1.clear();pb=0;
    while(t--)
    {
        char s[5];int x;scanf("%s",s+1);
        if(s[1]=='I'){scanf("%d",&x);q1.ins(x);}
        else if(s[1]=='D')q1.pop();//直接删除就好了 
        else if(s[1]=='L')
        {
            if(q1.p)//下标不可能为负数 
            {
                x=q1.top();b[++pb]=x;q1.change();q1.pop();
            }
        }
        else if(s[1]=='R')//不能超过当前最大位置 
        {
            if(pb)
            {
                x=b[pb];b[pb]=0;pb--;q1.ins(x);
            }
        }
        else {scanf("%d",&x);printf("%lld\n",q1.query(x));}
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/zyszlb2003/article/details/98300412