HDOJ 4699 Editor 栈 模拟
用两个栈模拟:
Editor
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1913 Accepted Submission(s): 591

8 I 2 I -1 I 1 Q 3 L D R Q 2
2 3 Hint The following diagram shows the status of sequence after each instruction:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <stack>
- #include <vector>
- using namespace std;
- const int INF=0x3f3f3f3f;
- const int maxn=1001000;
- int Left[maxn],Right[maxn];
- int sum[maxn],maxsum[maxn];
- int nl,nr;
- char op[10];
- int x,T_T,sz;
- void init()
- {
- nl=0; nr=0;
- maxsum[0]=-INF;
- sum[0]=0;
- sz=1;
- }
- int nextInt()
- {
- bool ok=false;
- int ret=0; char ch;
- int xi=0;
- while(ch=getchar())
- {
- if(ch=='-'||(ch>='0'&&ch<='9'))
- {
- ok=true;
- if(ch=='-') xi=1;
- else ret=ret*10+ch-'0';
- }
- else if(ok==true) break;
- }
- if(xi) ret*=-1;
- return ret;
- }
- char nextChar()
- {
- char ch=0;
- while(ch=getchar())
- {
- if(ch=='D'||ch=='R'||ch=='L'||ch=='Q'||ch=='I')
- {
- return ch;
- }
- }
- }
- int main()
- {
- while(scanf("%d",&T_T)!=EOF)
- {
- init();
- while(T_T--)
- {
- op[0]=nextChar();
- if(op[0]=='I')
- {
- x=nextInt();
- Left[nl++]=x;
- sum[sz]=sum[sz-1]+x;
- maxsum[sz]=max(maxsum[sz-1],sum[sz]);
- sz++;
- }
- else if(op[0]=='D')
- {
- if(nl==0) continue;
- nl--;
- sz--;
- }
- else if(op[0]=='L')
- {
- if(nl==0) continue;
- int t=Left[nl-1];
- nl--;
- Right[nr++]=t;
- sz--;
- }
- else if(op[0]=='R')
- {
- if(nr==0) continue;
- int t=Right[nr-1];
- nr--;
- Left[nl++]=t;
- sum[sz]=sum[sz-1]+t;
- maxsum[sz]=max(maxsum[sz-1],sum[sz]);
- sz++;
- }
- else if(op[0]=='Q')
- {
- int x;
- x=nextInt();
- printf("%d\n",maxsum[x]);
- }
- }
- }
- return 0;
- }
HDOJ 4699 Editor 栈 模拟的更多相关文章
- HDOJ 4699 Editor 对顶栈模拟
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- HDU/HDOJ 4699 Editor
对顶栈算法. 此题充分说明了cin的不中以及scanf的优越性. 我TM用cin超时了!!!换成scanf就A了!!! #include <cstdio> #include <cst ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- poj1363Rails(栈模拟)
主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
- 【LintCode·容易】用栈模拟汉诺塔问题
用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...
- 51Nod 1289 大鱼吃小鱼 栈模拟 思路
1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
随机推荐
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- mybatis逆向工程--自动生成实体代码(mybatis-generator)
随便找个目录, 添加文件, 如图 主要是两个jar包, generator的下载路径: https://github.com/mybatis/generator/releases 驱动包随 ...
- C++回调函数(callback)的使用
什么是回调函数(callback) 模块A有一个函数foo,他向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事 ...
- Latex Error cannot determine the size of graphic 报错的解决的方法
Latex Error cannot determine the size of graphic 报错的解决的方法 插入jpg文件老是会报错... 追究了半天,原来是编译的命令又问题,不应该使用 la ...
- cocos2d-x3.0 RichText
.h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...
- linux驱动移植的重要数据结构
转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201304/14-2538.html 对于嵌入式 Linux 系统来说,有各种体系结构的处理器和 ...
- 用最简单的例子理解适配器模式(Adapter Pattern)
中国足球的水平虽然不高,但实际上,在每个城市会有一批足球爱好者,他们踢球.看球.懂球.有这样的2个足球爱好者,一个是左脚选手,另一个是右脚选手. public class PlayWithLeft { ...
- MVC文件上传08-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹
当需要为每个用户建立一个专属上传文件夹的时候,可以在提交文件的视图中添加一个隐藏域,并设置name="objectContext". 相关兄弟篇: MVC文件上传01-使用jque ...
- Office Word等双击空白处的“隐藏的模块中的编译错误:MTW5”解决
Microsoft Visual Basic for Applications 隐藏的模块中的编译错误:MTW5. ...
- UCOS移植心得(
移植UCOS之前,你首先应该做好三件事: 1.弄懂UCOS,这是谁都知道的哦 ^_^ 2. 弄懂你想要移植到的硬件平台 3. 清楚你使用的编译器是如何处理函数的局部变量和怎么样处理函数间的参数传递 这 ...