用两个栈模拟:

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. oracle-systemtap

    https://github.com/hatem-mahmoud/scripts https://mahmoudhatem.wordpress.com/2015/05/26/stapora-v1-0- ...

  2. web office apps 在线预览实践

    摘要 在一些项目中需要在线预览office文档,包括word,excel,ppt等.达到预览文档的目的有很多方法,可以看我之前总结,在线预览的n种方案: [Asp.net]常见word,excel,p ...

  3. 用最简单的例子理解迭代器模式(Iterator Pattern)

    迭代器模式的需求来自:需要对一些集合进行迭代,而迭代的方式可能有很多种. 说到迭代,动作大致包括设置第一个位置,获取下一个位置元素,判断是否迭代结束,获取当前位置元素,大致就这么些.把这些迭代动作封装 ...

  4. Spring事务传播机制与隔离级别(转)

    Spring事务传播机制与隔离级别 博客分类: Spring   转自:http://blog.csdn.net/edward0830ly/article/details/7569954 (写的不错) ...

  5. mysql索引知识点汇总

    一.索引基础知识 1.什么叫数据库索引? 答:索引是对数据库中一列或者多列的值进行排序的一种数据结构.重点:对列的值进行排序的数据结构. 使用索引可以快速访问数据库中的记录 2.索引的主要用途是什么? ...

  6. JDBC连接oracle RAC数据库配置

    RAC的配置如下: node1:ip地址192.168.60.132,实例名:rac1,主机名:rac1 node2:ip地址192.168.60.144,实例名:rac2,主机名:rac2 RAC服 ...

  7. 《Windows核心编程》第九章——用内核对象进行线程同步

    先举一个有bug的例子: #include <iostream> #include <windows.h> #include <process.h> using n ...

  8. Android只播放gif动画

    使用easygifanimator软件把gif动画打散为图片. 第一步:先上图片素材,以下素材放到res/drawable目录下: 转:http://blog.csdn.net/aminfo/arti ...

  9. 使用HTML5画饼图

    在进行数据的统计分析时, 饼图也是比较经常用到的一类统计图. 需求分析:   一个饼图一般包含以下几部分:   1.标题   2.扇形   3.份额(百分比)   4.标识器      设计:     ...

  10. Objective-C:NSString类的常见用法

    几种常见的用法为:字符串的创建.字符串的搜索.字符串的比较.字符串的转换 用途一:字符串的创建 void ex1() { //1.常量字符串的对象 NSString *str1 = @"he ...