Input
8
I 2
I -1
I 1
Q 3
L
D
R
Q 2
Output
2
3
思路:就是模拟了,关键是怎么模拟了,这里用两个栈,如果左移就做边出栈,然后压到右边的栈。通过两个栈来模拟光标左右移动,其也就差不多了。
代码:
#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<stack>>
#define maxx 30005
using namespace std;
stack<int> s1,s2;
int sum[1000005];
int ans[1000005];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
while(s1.size())
s1.pop();
while(s2.size())
s2.pop();
char s[3];
int x;
while(n--)
{
scanf("%s",s);
if(s[0]=='I')
{
scanf("%d",&x);
s1.push(x);
sum[s1.size()]=sum[s1.size()-1]+x;
if(s1.size()==1)
ans[1]=sum[1];
else
ans[s1.size()]=max(ans[s1.size()-1],sum[s1.size()]);
}
else
if(s[0]=='Q')
{
scanf("%d",&x);
printf("%d\n",ans[x]);
}
else
if(s[0]=='D')
s1.pop();
else
if(s[0]=='L')
{
if(s1.size())
{
int x=s1.top();
s1.pop();
s2.push(x);
}
}
else
{
if(s2.size())
{
int x=s2.top();
s2.pop();
s1.push(x);
sum[s1.size()]=sum[s1.size()-1]+x;
if(s1.size()==1)
ans[1]=sum[1];
else
ans[s1.size()]=max(ans[s1.size()-1],sum[s1.size()]);
}
}
}
}
return 0;
}