P2201 数列编辑器

传送门呀呀呀呀呀呀呀呀呀呀呀呀呀

\(乍一看题目好像很难\)(实际也确实很难)

\(但是我们仔细看就发现,整个数列分成了光标前和光标后两组数列\)

\(我们有什么理由不分开储存呢??\)

\(然后光标移前移后无非是把光标前数组的最后一个数放在光标后数组的第一个数\)

\[\Huge\color{Purple}{这难道和栈不类似嘛!!} \]

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int a[maxn],b[maxn],topa,topb,n,S[maxn],ans[maxn];
int main()
{
	cin>>n;
	ans[0]=-999999999;
	for(int i=1;i<=n;i++)
	{
		char s;int w;
		cin>>s;
		if(s=='I')
		{
			scanf("%d",&w);
			a[++topa]=w;
			S[topa]=S[topa-1]+w;
			ans[topa]=max(ans[topa-1],S[topa]);
			
		}
		else if(s=='D')	a[topa--]=0;
		else if(s=='L')	b[++topb]=a[topa--];
		else if(s=='R')
		{
			a[++topa]=b[topb--];
			S[topa]=S[topa-1]+a[topa];
			ans[topa]=max(ans[topa-1],S[topa]);
		}
		else
		{
			scanf("%d",&w);//最大前缀和 
			printf("%d\n",ans[w]);
		}
	}
}
posted @ 2020-04-26 18:46  倾叶子佮  阅读(224)  评论(0编辑  收藏  举报