AcWing 128. 编辑器
原题链接
简单
作者:
虚实相依
,
2024-03-27 22:15:06
,
所有人可见
,
阅读 1
#include<iostream>
#include<stack>
#include<algorithm>
#include<cstdio>
#include<climits>
using namespace std;
const int N = 1e6 + 10;
int n;
stack<int> A;
stack<int> B;
int f[N];//保留第i个数前的最大前缀和
int sum[N];
int tl;//表示光标位置
int main()
{
cin >> n;
f[0]=INT_MIN;
while (n--)
{
getchar();//清空缓冲区中:一个指令后的保留的空格和回车
char c;
scanf("%c",&c);
if (c == 'I')
{
int val = 0;
cin >> val;
A.push(val);
tl++;
sum[tl] = sum[tl - 1] + val;
f[tl] = max(f[tl - 1], sum[tl]);
}
else if (c == 'L')
{
if(!A.empty())//注意范围条件,否则导致格式错误
{
auto tem = A.top();
B.push(tem);
A.pop();
tl--;
}
}
else if (c== 'D')
{
if(!A.empty())
{
tl--;
A.pop();
}
}
else if (c == 'R')
{
if(!B.empty())
{
auto tem = B.top();
B.pop();
A.push(tem);
tl++;
sum[tl] = sum[tl - 1] + tem;
f[tl] = max(f[tl - 1], sum[tl]);
}
}
else if(c =='Q')
{
int k = 0;
cin >> k;
cout << f[k]<<endl;
}
}
return 0;
}