Editor
Problem Description
“对顶栈”的思想十分巧妙,以cursor为中心,将左右的元素分别放入两个对顶栈中,合起来则为整个序列,以实现O(1)复杂度
我错了,本以为是道水题,然而一直WA,求助dalao
附上狂WA代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t,topa,topb,x,a[1000005],b[1000005],f[100005],sum[100005];
char c[5];
int main()
{
while(scanf("%d",&t)!=EOF)
{
memset(sum,0,sizeof(sum));
memset(f,0,sizeof(f));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
topa=0;topb=0;f[0]=-1e9;
while(t--)
{
scanf("%s",&c);
if(c[0]=='I')
{
scanf("%d",&x);
a[++topa]=x;
sum[topa]=sum[topa-1]+x;
f[topa]=max(f[topa-1],sum[topa]);
}
else if(c[0]=='D'){if(topa)topa--;}
else if(c[0]=='L')
{
if(!topb)continue;
b[++topb]=a[topa];topa--;
}
else if(c[0]=='R')
{
if(!topb)continue;
a[++topa]=b[topb];
topb--;
sum[topa]=sum[topa-1]+a[topa];
f[topa]=max(f[topa-1],sum[topa]);
}
else if(c[0]=='Q')
{
scanf("%d",&x);
printf("%d\n",f[x]);
}
}
}
return 0;
}