本题就是两个要点:

1.数据结构的设计。显然可以使用双向链表来做,但是写双向链表的代码复杂度高。其实更好的方法是使用两个对弹的栈来做,而且没必要用STL的栈,就自己开两个数组简单搞一下就好了。

2.最大前缀和的更新。很简单的递推关系,dp[i]=max(dp[i-1],sum[i]),意思是从开头到a[i]的最大前缀和里,要么没有i(即dp[i-1]),要么有i(即sum[i])。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<deque>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-;
const int INF=;
const int maxn=+; int n,x;
char op[];
int l[maxn],r[maxn],sum[maxn],dp[maxn]; int main()
{
//freopen("in2.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)==)
{
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(sum,,sizeof(sum));
memset(dp,,sizeof(dp));
dp[]=-INF;//这个一定要有
int t1=,t2=;
while(n--)
{
scanf("%s",op);
if(op[]=='I')
{
scanf("%d",&x);
l[++t1]=x;
sum[t1]=sum[t1-]+x;
dp[t1]=max(sum[t1],dp[t1-]);
}
else if(op[]=='L')
{
if(t1==) continue;
r[++t2]=l[t1--];
}
else if(op[]=='R')
{
if(t2==) continue;
l[++t1]=r[t2--];
sum[t1]=sum[t1-]+l[t1];
dp[t1]=max(sum[t1],dp[t1-]);
}
else if(op[]=='D')
{
if(t1==) continue;
t1--;
}
else if(op[]=='Q')
{
scanf("%d",&x);
printf("%d\n",dp[x]);
}
}
}
//fclose(stdin);
//fclose(stdout);
return ;
}

hdu4699 Editor(双向链表或双栈对弹)的更多相关文章

  1. NOIP2008双栈排序[二分图染色|栈|DP]

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  2. noip2008 双栈排序

    题目描述 Description \(Tom\)最近在研究一个有趣的排序问题.如图所示,通过\(2\)个栈\(S_1\)和\(S_2\),\(Tom\)希望借助以下\(4\)种操作实现将输入序列升序排 ...

  3. 双栈排序(codevs 1170)

    题目描述 Description Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈 ...

  4. #include <NOIP2008 Junior> 双栈排序 ——using namespace wxl;

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  5. [NOIP2008] 提高组 洛谷P1155 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  6. 【NOIP2008】双栈排序

    感觉看了题解还是挺简单的,不知道当年chty同学为什么被卡了呢么久--所以说我还是看题解了 原题: Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将 ...

  7. 双栈排序(codevs 1170)题解

    [问题描述] Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈 ...

  8. Noip2008双栈排序

    [问题描述] 用两个栈使一个1...n的排列变得有序.一共有四个操作: A.stack1.push() 读入一个放入栈一 B.stack1.pop() 弹出栈一放入输出序列 C.stack2.push ...

  9. NOIP2008 双栈队列

    1.      双栈排序 (twostack.pas/c/cpp) Tom 最近在研究一个有趣的排序问题.如图所示,通过 2 个栈 S1 和 S2,Tom 希望借助 以下 4 种操作实现将输入序列升序 ...

随机推荐

  1. .net Socket编程

    1.         什么是TCP/IP.UDP?2.         Socket在哪里呢?3.         Socket是什么呢?4.         你会使用它们吗? 什么是TCP/IP.U ...

  2. 九度OJ 1202:排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:19711 解决:6508 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). ...

  3. 【python】-- 函数非固定参数,返回值(return)

    函数非固定参数 1.默认参数: 代码如下: def information_register(name,age,country,sex): print("----注册信息------&quo ...

  4. Delphi 7里Messages.pas里所有104种重定义消息种类,180种不同的消息名称

    Delphi 7里Messages.pas里所有消息.经统计,共104种重定义消息种类,方便使用,180种不同的消息名称.省得像VC里一样,处处自己解析wParam和LParam参数进行分析.有空我要 ...

  5. cocos2dx的ui封装

    cocos2dx里加载cocosudio导出的ui配置文件,在这之上封装了一下,封装核心类包括 UIManager,UILayer,UIOwner UIManager是所有ui总的管理类,代码如下: ...

  6. 阿里云CentOS7安装Docker

    买了阿里云主机,由于学生有优惠,玩起来确实爽. 系统版本: [root@lxd ~]# cat /etc/redhat-release CentOS Linux release 7.0.1406 (C ...

  7. CentOS iSCSI服务器搭建------Target篇

    先上服务器信息(当然是我YY的服务器.哈哈) [root@node ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@node ...

  8. 关于scrollLeft的获取在不同浏览器或相同浏览器的不同版本下的获取

    chrome61向w3c规则靠拢,document.body.scrollLeft获取的值一直为0,需要使用document.documentElement.scrollLeft(或document. ...

  9. virtualbox 桥接 (转)

    virtualbox 自带的网络配置模式要么选择host-only,要么bridge,对于经常使用virtualbox的同学一定想要像vmware一样的nat配置,既可以让host访问guest,又可 ...

  10. 【leetcode刷题笔记】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...