您的位置:首页 > 其它

hdu 4699 Editor 多校第十场 (模拟)

2013-08-24 20:09 375 查看
栈 L,R 维护光标之前(之后)的序列

那么    I x        L.push(x);

            D       L.pop();

            L       R.push(L.pop());

           R       L.push(R.pop());

然后用  s[]数组维护前缀和,ans[]  数组维护前缀和的最大值

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1000010;
vector<int>L,R;
int s[maxn];
int ans[maxn];
int main()
{
int T;
while(~scanf("%d",&T))
{
L.clear();
R.clear();
char op[2];
int tot=0;
s[0]=0;

while(T--)
{
scanf("%s",op);
if(*op=='I')
{
int x;
scanf("%d",&x);
L.push_back(x);
s[tot+1]=s[tot]+x;
tot++;
if(tot==1) ans[tot]=s[tot];
else ans[tot]=max(s[tot],ans[tot-1]);
}
else if(*op=='D')
{
if(L.size()==0) continue;
L.pop_back();
tot--;
}
else if(*op=='L')
{
if(L.empty()) continue;
R.push_back(L.back());
L.pop_back();
tot--;
}
else if(*op=='R')
{
if(R.empty()) continue;
L.push_back(R.back());
s[tot+1]=s[tot]+R.back();
R.pop_back();
tot++;
ans[tot]=max(s[tot],ans[tot-1]);
}
else if(*op=='Q')
{
int k;
scanf("%d",&k);
printf("%d\n",ans[k]);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟