HDOJ 4699 Editor

Editor

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2818    Accepted Submission(s): 825


Problem Description
 

Sample Input
  
  
8 I 2 I -1 I 1 Q 3 L D R Q 2
 

Sample Output
  
  
2 3
Hint
The following diagram shows the status of sequence after each instruction:
 

题意:5种操作,在下标后面加一个数,删除下标前面的一个数,将下标左右移动,求下标从1-k连续子区间和最大。
用两个栈来维护,一个栈来维护下标前面,另一个栈来维护下标后面,然后对第一个栈,增加或者减少数来更新ans数组就行了。

#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<stack>
#include<math.h>
using namespace std;
int a[1000005];
int main()
{
    char ss[200];
    char s;int q;
    while(scanf("%d",&q)!=EOF)
    {
        stack<int>s1;
        stack<int>s2;
        int x;
        int ans=0;
        a[0]=-999999999;
        while(q--)
        {
            scanf("%s",ss);
            s=ss[0];
            if(s=='I')
            {
                scanf("%d",&x);
                s1.push(x);
                ans+=x;
                int len=s1.size();
                a[len]=max(ans,a[len-1]);
            }
            else if(s=='D')
            {
                if(s1.size()>=1)
                {
                    x=s1.top();
                    s1.pop();
                    ans-=x;
                }
            }
            else if(s=='L')
            {
                if(s1.size()>=1)
                {
                    x=s1.top();
                    s1.pop();
                    ans-=x;
                    s2.push(x);
                }
            }
            else if(s=='R')
            {
                if(s2.size()>=1)
                {
                    x=s2.top();
                    s2.pop();
                    s1.push(x);
                    ans+=x;
                    int len=s1.size();
                    a[len]=max(ans,a[len-1]);
                }
            }
            else if(s=='Q')
            {
                int k;
                scanf("%d",&k);
                printf("%d\n",a[k]);
            }
        }
    }

return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值