Editor
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3907 Accepted Submission(s): 1063
Problem Description
Sample Input
8 I 2 I -1 I 1 Q 3 L D R Q 2
Sample Output
2 3
Hint
The following diagram shows the status of sequence after each instruction:
一、原题地址
二、大致题意
5种操作,看hint就能懂题意。
三、思路
用dp[n]维护前缀和,ans[n]维护前n项的最大和段。
四、代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
int Q, num, cur;
char type;
stack<int>a, b;
int dp[1000005], ans[1000005];
int main()
{
while (~scanf("%d", &Q))
{
getchar();
cur = 1;
dp[0] = 0;
ans[0] = -INF;
while (!a.empty())a.pop();
while (!b.empty())b.pop();
while (Q--)
{
scanf("%c", &type);
if (type == 'I')
{
scanf("%d", &num);
getchar();
dp[cur] = dp[cur - 1] + num;
ans[cur] = max(dp[cur], ans[cur - 1]);
cur++;
a.push(num);
}
else if (type == 'D')
{
getchar();
cur--;
a.pop();
}
else if (type == 'L')
{
getchar();
if (!a.empty())
{
int t = a.top();
a.pop();
cur--;
b.push(t);
}
}
else if (type == 'R')
{
getchar();
if (!b.empty())
{
int t = b.top();
b.pop();
dp[cur] = dp[cur - 1] + t;
ans[cur] = max(dp[cur], ans[cur - 1]);
cur++;
a.push(t);
}
}
else if (type == 'Q')
{
scanf("%d", &num);
getchar();
printf("%d\n", ans[num]);
}
}
}
getchar();
getchar();
}