题目大意:按照题目所给操作维护一个整数序列编辑器。
大致思路:建立两个栈A,B。A栈存储序列开头到当前光标位置的这一段子序列,B存储当前光标位置到序列结尾这一段子序列。用数组f维护栈A的前缀和的最大值,sum用来记录前缀和的值。对于操作I,把x插入栈A,并且更新f[pa],sum[pa]的值,对于操作D,把A的栈顶出栈,对于操作R,弹出B的栈顶插入到A中并且更新f[pa],sum[pa]。
#include <bits/stdc++.h>
using namespace std;
const int INF = -0x3f3f3f3f;
int sum[10000006];
int f[10000006];
int main(int argc, char const *argv[])
{
int Q,x,k;
char option;
while(~scanf("%d",&Q)){
k = 0;
memset(f,INF,sizeof(f));
memset(sum,0,sizeof(sum));
stack<int> stA;
stack<int> stB;
while(Q--){
getchar();
scanf("%c",&option);
if(option == 'I'){
scanf("%d",&x);
stA.push(x);
k++;
sum[k] = sum[k - 1] + x;
f[k] = max(f[k - 1],sum[k]);
}
else if(option == 'D'){
if(stA.empty()) continue;
stA.pop();
k--;
}
else if(option == 'L'){
if(stA.empty()) continue;
int a = stA.top();
stA.pop();
stB.push(a);
k--;
}
else if(option == 'R'){
if(stB.empty()) continue;
int b = stB.top();
stB.pop();
stA.push(b);
k++;
sum[k] = sum[k - 1] + b;
f[k] = max(f[k - 1],sum[k]);
}
else if(option == 'Q'){
int cur;
scanf("%d",&cur);
printf("%d\n",f[cur]);
}
}
}
return 0;
}