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 ...
随机推荐
- Running multiple instances of Xamarin Studio on a Mac
I love developing software on my MacBook Air! I got the latest version with the maximum possible spe ...
- javax.mail 遇到501 mail from address must be same as authorization user 的問題
使用不同的兩個帳戶发送email时,第一个账户可以发送成功,但到第二个账户的时候就报出了501 mail from address must be same as authorization user ...
- table固定首行(一)
<html> <head> <title>Untitled Document</title> <meta http-equiv="Con ...
- [SQLite] SQLite学习手册(数据库和事务)
转载地址:http://www.cnblogs.com/stephen-liu74/archive/2012/02/18/2322575.html 一.Attach数据库: ATTACH DATABA ...
- avi视频格式转yuv格式与播放yuv视频
因为要用到yuv格式视频.而眼下仅仅有avi格式的视频,所以须要转换,而且opencv不支持yuv编码的视频播放.所以须要转换为rgb编码.而后播放.写了两个程序.以供參考: 1,avi格式视频转yu ...
- html 制作复杂table
数据分析,一般都需要显示数据,就需要使用html做复杂的表格.复杂表格一般是对td的rowspan .colspan属性值. 在html中<td> 标签定义 HTML 表格中的标准单元格. ...
- C语言获取系统当前时间
C语言获取系统当前时间 time_t -- 时间类型 struct tm -- 时间结构 time(&now)函数获取当前时间距1970年1月1日的秒数,以秒计数单位. localtime ( ...
- css美化、优化、合并工具推荐
其实很多时候,我们写完css规则之后,我们思考的无非就是3件事情: 验证 美化 压缩 当然无论是我们自己做这样的工具还是寻找一些比较好的成熟的工具,都有几个期望: 是否支持一些ie下的hack方式: ...
- 遇到问题描述:Android Please ensure that adb is correctly located at问题解决
遇到问题描述: 运行android程序控制台输出 [2013-11-04 16:18:26 - ] The connection to adb is down, and a severe error ...
- Shape of HDU_hdu_2108(几何).java
Shape of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...