题目:
思路:
直接暴力50pts
栈+模拟对于每一个字母模拟每一个操作
当字母为I就插入数字
if (ch == 'I')
{
y = read1();//读入数字
a[++a[0]] = y;//将 y 放入 栈前面
qzh[a[0]] = qzh[a[0] - 1] + y;//求前缀和
ans[a[0]] = max (ans[a[0] - 1], qzh[a[0]]);//维护
}
当字母为D就删除
if (ch == 'D')
{
a[0]--;//删除
}
当字母为L或R就移动
if (ch == 'L')
{
b[++b[0]] = a[a[0]];
a[0]--;
}
if (ch == 'R')
{
a[++a[0]] = b[b[0]];
qzh[a[0]] = qzh[a[0] - 1] + a[a[0]];
ans[a[0]] = max (ans[a[0] - 1],qzh[a[0]]);
b[0]--;
}e
当字母为Q就输入输出
if (ch == 'Q')
{
y = read1 ();
printf("%d\n", ans[y]);
}
C o d e Code Code:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int ans[1000005],qzh[1000005],a[1000005],b[1000005];
int n,i,y;
char ch;
int read1() //快读(数字)
{
int x=0,flag=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')flag=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*flag;
}
char read2() //快读(字符)
{
char ch = getchar ();
while (ch != 'I' && ch != 'D' && ch != 'L' && ch != 'R' && ch != 'Q')
ch = getchar ();
return ch;
}
int main ()
{
ans[0] = -0x3f3f3f3f;//先赋1个小值
n = read1 ();
while(i < n)//如思路
{
ch = read2 ();
if (ch == 'I')
{
y = read1();
a[++a[0]] = y;
qzh[a[0]] = qzh[a[0] - 1] + y;
ans[a[0]] = max (ans[a[0] - 1], qzh[a[0]]);
}else
if (ch == 'D')
{
a[0]--;
}else
if (ch == 'L')
{
b[++b[0]] = a[a[0]];
a[0]--;
}else
if (ch == 'R')
{
a[++a[0]] = b[b[0]];
qzh[a[0]] = qzh[a[0] - 1] + a[a[0]];
ans[a[0]] = max (ans[a[0] - 1],qzh[a[0]]);
b[0]--;
}else
if (ch == 'Q')
{
y = read1 ();
printf("%d\n", ans[y]);
}
++i;
}
return 0;
}