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
3HintThe 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 ...
随机推荐
- Ubuntu下(Linux+Apache+MYSQL+PHP, LAMP)环境搭建
近期開始玩PHP,于是试着搭建一下开发环境并做个记录,以备日后再使用起来方便可查. 第一步 确保软件包是最新的 sudo apt-get update 第二步 安装Apache2 sudo apt-g ...
- memcached-session-manager配置
原文地址: http://chenzhou123520.iteye.com/blog/1650212 声明:本篇文章是根据memcached-session-manager官方配置方法wiki页面翻译 ...
- 浅用block 转
block是一门有用的大后期学问.现在我只是列出一点基本用法. 1.快速枚举(Enumeration) 通常是和NSArray, NSDictionary, NSSet, NSIndexSet放在一起 ...
- 10.2.2移动产品离线功能等具体解释----暨4月8日移动《在离线一体化》公开课Q&A
4月8日<离,或者不离,ArcGIS移动的"在离线一体化"就在那里!>移动公开课已经结束,针对公开课上粉丝们重点关注的问题,本博客进行了具体的解答.答疑主要环绕最新的R ...
- VC调用QT的UIDLL
//VC程序#include "../QTDLL/ExportDll.h" int _tmain(int argc, _TCHAR* argv[]) { printf(" ...
- [JQuery] jQuery选择器ID、CLASS、标签获取对象值、属性、设置css样式
reference : http://www.suyunyou.com/aid1657.html jQuery是继prototype之后又一个优秀的Javascrīpt框架.它是轻量级的js库(压缩后 ...
- SQLAlchemy 操作方法汇总
参考 1.查询: #简单查询 print(session.query(User).all()) print(session.query(User.name, User.fullname).all()) ...
- 网络编程socket之listen函数
摘要:listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被 ...
- Mysql数据库事务及隔离级别学习测试
参考了这篇文章的一些内容: http://xm-king.iteye.com/blog/770721 记住以下这张表: 我在springdemo库里面建了一个表: CREATE TABLE `tx` ...
- 运维-JVM监控之内存泄漏
转载:https://blog.csdn.net/zdx_csdn/article/details/71214219 jmap -heap pid查看进程堆内存使用情况,包括使用的GC算法.堆配置参数 ...