hdu 4699 Editor (巧用两个栈)
来源:互联网 发布:淘宝jordan旗舰店真假 编辑:程序博客网 时间:2024/06/10 22:58
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4699
题意:
Editor
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2306 Accepted Submission(s): 704
Problem Description
Sample Input
8I 2I -1I 1Q 3LDRQ 2
Sample Output
23HintThe following diagram shows the status of sequence after each instruction:
Source
2013 Multi-University Training Contest 10
分析:用两个栈存数,光标始终在第一个栈的栈顶,左移s1就向s2里面弹一个数,右移s2就向s1里面弹一个值,插入和删除直接在s1的栈顶操作。找最大前缀和,可以用两个数组维护,一个记录前缀和,一个记录最大前缀和,当增加一个数,最大前缀和maxpre[k]只可能是maxpre[k-1]和pre[k]里面的最大值。
代码:
#include <iostream>#include <cstring>#include <cstdio>#include <stack>using namespace std;const int maxn = 1e6+7;const int INF = 2E9;int pre[maxn],maxpre[maxn],cur,x,q;stack <int > s1,s2;void Init(){maxpre[0]=pre[0]=-INF;cur=1;while(!s1.empty())s1.pop();while(!s2.empty())s2.pop();}void Insert(){scanf("%d",&x);s1.push(x);pre[cur]=pre[cur-1]+x;if(cur==1)pre[cur]=x;maxpre[cur]=max(maxpre[cur-1],pre[cur]);cur++;}void Delete(){if(s1.empty())return ;s1.pop();cur--;}void Lmove(){if(s1.empty())return ;int temp=s1.top();s1.pop();s2.push(temp);cur--;}void Rmove(){if(s2.empty())return ;int temp=s2.top();s2.pop();s1.push(temp);pre[cur]=pre[cur-1]+temp;if(cur==1)pre[cur]=temp;maxpre[cur]=max(maxpre[cur-1],pre[cur]);cur++;}void Query(){scanf("%d",&x);printf("%d\n",maxpre[x]);}int main(){char str[3];while(scanf("%d",&q)!=EOF){Init();while(q--){scanf("%s",str);switch(str[0]){case 'I':Insert();break;case 'D':Delete();break;case 'L':Lmove();break;case 'R':Rmove();break;case 'Q':Query();break;default:break;}}}return 0;}
0 0
- hdu 4699 Editor (巧用两个栈)
- hdu 4699 Editor(单调栈)
- hdu 4699 Editor
- HDU 4699 Editor
- hdu 4699 Editor 模拟
- hdu 4699 Editor(Splay)
- Hdu 4699 Editor(Splay)
- HDU 4699 Editor( stack)
- HDU 4699 Editor 树状数组
- HDU 4699 Editor (双栈)
- HDU 4699 Editor (栈的使用,模拟)
- hdu 4699 Editor 双栈维护最长前缀
- hdu 4699 Editor(splay tree 伸展树)
- hdu 4699 Editor 伸展树 treap复习
- HDOJ 4699 Editor 栈 模拟
- hdu 4699 Editor 多校第十场 (模拟)
- hdu 4699 Editor(双向链表+随便维护前缀和)
- 2013 多校第九场 hdu 4699 Editor(vector OR splay tree)
- Java--线程池
- 文章标题
- HTTP 错误 401.2 - Unauthorized
- proxy-target-class="true" 与proxy-target-class="false"的区别
- DateUtil中一些有用的方法
- hdu 4699 Editor (巧用两个栈)
- Matlab给曲线添加加参考线
- ACM的你伤不起!!!
- Linux线程属性总结(一)
- RTP 与RTCP 解释. 含同步时间戳
- java学习笔记StringBuffer/StringBuilder及基本数据类型
- 百度地图坐标拾取+覆盖物+逆解析
- std::string find函数
- EL表达式字符串操作