用两个栈模拟:

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
8
I 2
I -1
I 1
Q 3
L
D
R
Q 2
 
Sample Output
2
3
Hint
The following diagram shows the status of sequence after each instruction:
 
Source
 

#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 栈 模拟的更多相关文章

  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. intrawebIW当作REST 服务端

    intraweb15 该版本支持HTTPS.SYS通信. IW当作REST 服务端使用: procedure TIWServerController.IWServerControllerBaseExe ...

  2. Glusterfs初试

    Gluster的模式及介绍在此不表,这里只记录安装及配置过程. 1.整体环境 server1 : gfs1.cluster.com server2 : gfs2.cluster.com Client: ...

  3. 深度学习材料:从感知机到深度网络A Deep Learning Tutorial: From Perceptrons to Deep Networks

    In recent years, there’s been a resurgence in the field of Artificial Intelligence. It’s spread beyo ...

  4. 局部敏感哈希 Kernelized Locality-Sensitive Hashing Page

    Kernelized Locality-Sensitive Hashing Page   Brian Kulis (1) and Kristen Grauman (2)(1) UC Berkeley ...

  5. Java:字符串类简单的正则表达式

    class Test { public static void main(String[] args) { String str = "xia..as....yuan.com"; ...

  6. DDR SDRAM芯片DQS的作用以及读写DQS/DQ对齐方式不同的原因

    节选内容转载自https://www.design-reuse.com/articles/13805/the-love-hate-relationship-with-ddr-sdram-control ...

  7. Apache PHP Mysql 开发环境快速配置

    学习PHP开发要配置各种环境,一般会用到apache作为服务器.Mysql数据库.如何快速的配置环境成为困扰大家的烦恼,之前自己也配过,比较繁琐. 最新发现一款集成安装软件“phpStudy”.真可谓 ...

  8. go语言之进阶篇指针类型和普通类型的方法集

    方法集 类型的方法集是指可以被该类型的值调用的所有方法的集合. 用实例实例 value 和 pointer 调用方法(含匿名字段)不受方法集约束,编译器编总是查找全部方法,并自动转换 receiver ...

  9. iOS开发-plist文件增删改查

    plist第一次看到这个后缀名文件的时候感觉怪怪的,不过接触久了也就习以为常了,plist是Property List的简称可以理解成属性列表文件,主要用来存储串行化后的对象的文件.扩展名为.plis ...

  10. IE浏览器报Promise未定义的错误、解决vuex requires a Promise polyfill in this browser问题

    一个vue-cli构建的vue项目,一个使用angular的项目,两个项目在其他浏览器一切正常,但是ie中会报Promise未定义的错误 解决办法: 一.vue的项目: 1.npm install b ...