hdu 4699 双栈模拟

http://acm.hdu.edu.cn/showproblem.php?pid=4699

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:
 


/**
hdu 4699 双栈模拟
题目大意:给出一系列操作模拟光标:I在光标左边插入一个数,
                                    D删除光标左边的数
                                    L将光标移动最左边
                                    R将光标移动到最右边
                                    Q k 询问k位置以前的最大前缀和
解题思路:定义两个栈,一个从前开始,一个从后开始,二者加起来就是整个数列。至于最大前缀和用dp维护一下就好了
           dp[i]=(dp[i-1],sum[i]);sum数组里存储的是1~i的和
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e6+5;

int dp[maxn],sum[maxn];
int n,s1[maxn],s2[maxn];

int main()
{
    while(~scanf("%d",&n))
    {
        int l=0,r=0;
        dp[0]=-10000000;
        sum[0]=0;
        for(int i=0; i<n; i++)
        {
            char s[5];
            scanf("%s",s);
            if(s[0]=='I')
            {
                scanf("%d",&s1[++l]);
                sum[l]=sum[l-1]+s1[l];
                dp[l]=max(dp[l-1],sum[l]);
            }
            else if(s[0]=='D')
            {
                l--;
            }
            else if(s[0]=='L')
            {
                if(l!=0)
                    s2[++r]=s1[l--];
            }
            else if(s[0]=='R')
            {
                if(r!=0)
                {
                    s1[++l]=s2[r--];
                    sum[l]=sum[l-1]+s1[l];
                    dp[l]=max(dp[l-1],sum[l]);
                }
            }
            else
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",dp[x]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值