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 3HintThe 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;
}