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 ...
随机推荐
- C#程序集系列08,设置程序集版本
区别一个程序集,不仅仅是程序集名称,还包括程序集版本.程序集公匙.程序集文化等,本篇体验通过界面和编码设置程序集版本. □ 通过Visual Studio设置程序集版本 →右键项目,选择"属 ...
- 平时常用的Visual Studio操作技巧,持续更新中……
移除未使用的命名空间--方法1:右键--"组织using"--"移除未使用的using"--方法2:Shift+F10--"O"-" ...
- 浅谈Android RecyclerView
Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...
- Android 面试题(答案最全)
1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚拟 ...
- Android读取Excel文件
转:http://bigcat.easymorse.com/?p=1648 java可以读取Excel文件,android同样也行,效果如下: excel源文件: 读取日志如下: 首先需要引入jxl. ...
- pgm转jpg
clc;clear all;for i=1:40for j=1:10image=imread(strcat('N:\FACE\orl_faces\s',...int2str(i),'\',int2st ...
- [PHP] Ubuntu 16.10 开启PHP错误提示
两个步骤: 修改php.ini配置文件中的error_reporting 和 display_errors两地方内容: sudo vim /etc/php/7.0/apache2/php.ini er ...
- Jquery Mobile实例--利用优酷JSON接口读取视频数据
本文将介绍,如何利用JqueryMobile调用优酷API JSON接口显示视频数据. (1)注册用户接口. 首页,到 http://open.youku.com 注册一个账户,并通过验证.然后找到A ...
- libcurl使用easy模式阻塞卡死等问题的完美解决---超时设置
libcurl使用时疑难问题: 在使用libcurl时, jwisp发现, curl_easy_perform是阻塞的方式进行下载的, curl_easy_perform执行后,程序会在这里阻塞等待下 ...
- iOS开发-图片查看(ScrollView+UIPageControl)
上周没事写了一个简单的图片查看,上次的查看只用到了一个UIImageView,不断的替换背景图片,实现图片之间的切换.通过ScrollView可以很简单的是实现图片之间的查看,设置setPagingE ...