http://acm.hdu.edu.cn/showproblem.php?pid=4699
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/b85340f291714255f8f4a743cb84930b.jpeg)
Sample Input
8 I 2 I -1 I 1 Q 3 L D R Q 2
Sample Output
2 3HintThe 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;
}