用两个栈模拟:

Editor

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1913    Accepted Submission(s): 591

Problem Description
 
Sample Input
  1. 8
  2. I 2
  3. I -1
  4. I 1
  5. Q 3
  6. L
  7. D
  8. R
  9. Q 2
 
Sample Output
  1. 2
  2. 3
  3. Hint
  4. The following diagram shows the status of sequence after each instruction:
  5.  
 
Source
 

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <stack>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int INF=0x3f3f3f3f;
  11. const int maxn=1001000;
  12.  
  13. int Left[maxn],Right[maxn];
  14. int sum[maxn],maxsum[maxn];
  15. int nl,nr;
  16.  
  17. char op[10];
  18. int x,T_T,sz;
  19.  
  20. void init()
  21. {
  22. nl=0; nr=0;
  23. maxsum[0]=-INF;
  24. sum[0]=0;
  25. sz=1;
  26. }
  27.  
  28. int nextInt()
  29. {
  30. bool ok=false;
  31. int ret=0; char ch;
  32. int xi=0;
  33. while(ch=getchar())
  34. {
  35. if(ch=='-'||(ch>='0'&&ch<='9'))
  36. {
  37. ok=true;
  38. if(ch=='-') xi=1;
  39. else ret=ret*10+ch-'0';
  40. }
  41. else if(ok==true) break;
  42. }
  43. if(xi) ret*=-1;
  44. return ret;
  45. }
  46.  
  47. char nextChar()
  48. {
  49. char ch=0;
  50. while(ch=getchar())
  51. {
  52. if(ch=='D'||ch=='R'||ch=='L'||ch=='Q'||ch=='I')
  53. {
  54. return ch;
  55. }
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. while(scanf("%d",&T_T)!=EOF)
  62. {
  63. init();
  64. while(T_T--)
  65. {
  66. op[0]=nextChar();
  67. if(op[0]=='I')
  68. {
  69. x=nextInt();
  70. Left[nl++]=x;
  71. sum[sz]=sum[sz-1]+x;
  72. maxsum[sz]=max(maxsum[sz-1],sum[sz]);
  73. sz++;
  74. }
  75. else if(op[0]=='D')
  76. {
  77. if(nl==0) continue;
  78. nl--;
  79. sz--;
  80. }
  81. else if(op[0]=='L')
  82. {
  83. if(nl==0) continue;
  84. int t=Left[nl-1];
  85. nl--;
  86. Right[nr++]=t;
  87. sz--;
  88. }
  89. else if(op[0]=='R')
  90. {
  91. if(nr==0) continue;
  92. int t=Right[nr-1];
  93. nr--;
  94. Left[nl++]=t;
  95. sum[sz]=sum[sz-1]+t;
  96. maxsum[sz]=max(maxsum[sz-1],sum[sz]);
  97. sz++;
  98. }
  99. else if(op[0]=='Q')
  100. {
  101. int x;
  102. x=nextInt();
  103. printf("%d\n",maxsum[x]);
  104. }
  105. }
  106. }
  107. return 0;
  108. }

HDOJ 4699 Editor 栈 模拟的更多相关文章

  1. HDOJ 4699 Editor 对顶栈模拟

    Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  2. HDU/HDOJ 4699 Editor

    对顶栈算法. 此题充分说明了cin的不中以及scanf的优越性. 我TM用cin超时了!!!换成scanf就A了!!! #include <cstdio> #include <cst ...

  3. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  4. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  5. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  6. poj1363Rails(栈模拟)

    主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...

  7. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  8. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  9. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

随机推荐

  1. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  2. mybatis逆向工程--自动生成实体代码(mybatis-generator)

    随便找个目录,  添加文件, 如图 主要是两个jar包,  generator的下载路径:   https://github.com/mybatis/generator/releases   驱动包随 ...

  3. C++回调函数(callback)的使用

    什么是回调函数(callback)    模块A有一个函数foo,他向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事 ...

  4. Latex Error cannot determine the size of graphic 报错的解决的方法

    Latex Error cannot determine the size of graphic 报错的解决的方法 插入jpg文件老是会报错... 追究了半天,原来是编译的命令又问题,不应该使用 la ...

  5. cocos2d-x3.0 RichText

    .h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...

  6. linux驱动移植的重要数据结构

    转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201304/14-2538.html 对于嵌入式 Linux 系统来说,有各种体系结构的处理器和 ...

  7. 用最简单的例子理解适配器模式(Adapter Pattern)

    中国足球的水平虽然不高,但实际上,在每个城市会有一批足球爱好者,他们踢球.看球.懂球.有这样的2个足球爱好者,一个是左脚选手,另一个是右脚选手. public class PlayWithLeft { ...

  8. MVC文件上传08-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹

    当需要为每个用户建立一个专属上传文件夹的时候,可以在提交文件的视图中添加一个隐藏域,并设置name="objectContext". 相关兄弟篇: MVC文件上传01-使用jque ...

  9. Office Word等双击空白处的“隐藏的模块中的编译错误:MTW5”解决

    Microsoft Visual Basic for Applications 隐藏的模块中的编译错误:MTW5. ...

  10. UCOS移植心得(

    移植UCOS之前,你首先应该做好三件事: 1.弄懂UCOS,这是谁都知道的哦 ^_^ 2. 弄懂你想要移植到的硬件平台 3. 清楚你使用的编译器是如何处理函数的局部变量和怎么样处理函数间的参数传递 这 ...