Editor

时间限制: 1 Sec  内存限制: 128 MB
提交: 8  解决: 5
[提交] [状态] [讨论版] [命题人:admin]

题目描述

You are going to implement the most powerful editor for integer sequences.
The sequence is empty when the editor is initialized.
There are 5 types of instructions.

I x Insert x after the cursor.
D Delete the element before the cursor.
L Move the cursor left unless it has already been at the begin.
R Move the cursor right unless it has already been at the end.
Q k Suppose that the current sequence BEFORE the cursor is {a1,a2,...,an}.Find max1≤i≤k Si where Si=a1+a2+...+ai.

输入

The input file consists of multiple test cases.For eache test case:
The first line contains an integer Q,which is the number of instructions.The next Q lines contain an instruction as described above。
(1≤Q≤106,|x|≤103 for I instruction,1≤k≤n for Q instruction)

输出

For eache Q instruction,output the desired value.

样例输入

8
I 2
I -1
I 1
Q 3
L
D
R
Q 2

样例输出

2
3

提示

The following diagram shows the status of sequence after each instruction:

思路:
根据题意要求实现对顶栈,同时更新光标前的栈的最大值。
代码如下:

#include <bits/stdc++.h>using namespace std;
const int maxn = 1e6 + 10;
stack<int> before, after;
int sum = 0, f[maxn], n, x, pn;
char op[2];int main() {scanf("%d", &n);f[0]=-0x3f3f3f3f,sum=0;while (n--) {scanf("%s", op);if (op[0] == 'I') {scanf("%d", &x);before.push(x);sum += x;pn=before.size();f[pn] = max(f[pn - 1], sum);
//            printf("%d %d %d\n",pn,sum[pn],f[pn]);} else if (op[0] == 'D') {if (before.size() < 1) continue;x = before.top();before.pop();sum -= x;} else if (op[0] == 'L') {if (before.size() < 1) continue;x = before.top();before.pop();after.push(x);sum -= x;} else if (op[0] == 'R') {if (after.size() < 1) continue;x = after.top();after.pop();before.push(x);sum += x;pn=before.size();f[pn] = max(f[pn - 1], sum);} else {scanf("%d", &x);printf("%d\n", f[x]);}}return 0;
}

View Code

转载于:https://www.cnblogs.com/acerkoo/p/9539700.html

HDU 4699 Editor (对顶栈)相关推荐

  1. hdu 4699 Editor(splay tree 伸展树)

    hdu 4699  Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 解题思路:标乘是用了栈进行维 ...

  2. hdu 4699 2个栈维护 or 伸展树 (2013多校联合)

    hdu 4699  Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求1到k位置的最大的前缀和.. 注意这里的k是在光标之前的 ...

  3. Aizu - 1407 Parentheses Editor(对顶栈+模拟)

    题目链接:点击查看 题目大意:给出一个字符串,只由 ' ( ' , ' ) ' 和 ' - ' 组成,初始时给出一个空串 s,三种字符所代表的操作如下: ' ( ' :在 s 后添加一个左括号 ' ) ...

  4. hdu 4699 Editor(Splay)

    题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和. Splay在比赛的时候写得太Navie,T了整场. 左移和右移的 ...

  5. 【HDOJ4699】Editor(对顶栈,模拟)

    problem 维护一个整数序列的编辑器,支持5种操作,操作数< 1e6 I x:在光标后插入数x,插入后光标移到x后 D:删除光标前的一个整数 L:光标左移一个位置 R:光标右移一个位置 Q ...

  6. Editor HDU 4699

    题目 #include <iostream> using namespace std;const int N = 1e6 + 10; int p[2], a[N], b[N], sum[N ...

  7. hdu4699-Editor【对顶栈】

    正题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4699 大意 有5种操作 1.在光标处写入一个数字 2.在光标处删除一个数字 3.4.将光标往 左 ...

  8. HDU 4286 Data Handler [栈,双端队列]

    这题比较容易想到的做法是splay,但是splay写起来比较麻烦而且每次操作都有LogN的复杂度,双向链表也是可以实现的,但实践起来比较麻烦,尤其是翻转操作... 可以发现每次L或者R都是移动一位的, ...

  9. HDU - 5875 Function(单调栈)

    题目链接:点击查看 题目大意:给出一段连续数列,在给出m个询问,要求按照给出的函数查询得到结果 题目分析:第一眼一看题目会觉得是个递归题目,但是盲目递归肯定会TLE,所以我们要分析这个题目到底要干什么 ...

  10. 《算法竞赛进阶指南》刷题记录

    总算闲下来一些辣!然后最近发现其实看书是真真很有效但是一直没有落实!所以决定落实一下这段时间把这本书看完题目做完! 然后发现还有挺多题目挺巧妙的于是一堆博客预警,,,可能最近会写很多比较水(但是我还是 ...

最新文章

  1. Scenario 7 – HP C7000 VC FlexFabric Tunneled VLANs and SUS A/A vSphere
  2. Python--切片学习记录
  3. Linux 搭建Sphinx 全文检索引擎
  4. C语言1e12怎么识别,掌握C语言中基本的运算符
  5. Mybatis中resultMap使用
  6. yii之behaviors
  7. C# 二进制BinaryFormatter进行序列化与反序列化
  8. 智能机器人建房子后房价走势_人工智能未来10年将颠覆房地产行业,你还敢买房吗?...
  9. Bailian3246 展览会【计数+求和】
  10. 转:oracle常见重要视图-v$sql,v$sql_plan,v$sqltext,v$sqlarea,v$sql_plan_statistcs
  11. 地震及断层分析相关软件
  12. 【Python】二分法求函数零点
  13. HDU - How far away ?(DFS+vector)
  14. 丰巢刷脸取件被小学生破解,号称3D、深度学习加持的人脸识别究竟靠谱吗?...
  15. 代码查重——LCS、Levenshtein距离、Jaro-Whinkle距离和变量代换
  16. 微信小程序实现长文本分段播放
  17. elementUI表格无数据显示图片
  18. MySQL——使用mysqldump命令备份
  19. MySQL 1054错误 Unknown column .... in 'on clause'
  20. 【解题报告】2017-2018 8th BSUIR Open Programming Contest-C Good subset 线性基+线段树

热门文章

  1. android textview字体为宋体,安卓开发--textView的字体样式设置(设置宋体,微软雅黑等)...
  2. Vue项目url中的BASE_URL解析
  3. 爬虫项目:大麦网分析
  4. html图片滚动首尾互联,Flash几张图片首尾连接循环滚动
  5. 一个计算机专业女孩的求学之路——七年之痒,痒之感悟
  6. java 句柄无效_c# – 通过java运行.net控制台时“句柄无效”
  7. 民科微服务电脑版下载_民科微服务电子注册护士个人端下载-民科微服务医师电子注册app最新版 v2.4_爱下手机站...
  8. adb连接木木模拟器
  9. JavaScript 数字转成大写“零壹贰叁肆伍陆柒捌玖”的方法
  10. Flink 实战问题(五):The transaction timeout is larger than the maximum value allowed by the broker