HDU-4699-Editor(栈+前缀和)

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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值