HDOJ 4699-Editor[栈]
生活随笔
收集整理的這篇文章主要介紹了
HDOJ 4699-Editor[栈]
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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:
.
.
.
.
.
分析
5種操作,在光標(biāo)后面加一個(gè)數(shù),刪除光標(biāo)前面的一個(gè)數(shù),將光標(biāo)左右移動(dòng),求光標(biāo)從1-k連續(xù)子區(qū)間和最大。
用兩個(gè)棧來維護(hù),一個(gè)棧來維護(hù)光標(biāo)前面,另一個(gè)棧來維護(hù)光標(biāo)后面,然后對(duì)第一個(gè)棧,增加或者減少數(shù)來更新f數(shù)組就行了。
.
.
.
.
.
程序:
#include<iostream> #include<stack> #include<stdio.h> using namespace std; int f[2000000];int main() {char ch,zfc[200];int n;while (scanf("%d",&n)!=EOF) {stack <int> a;stack <int> b;int sum=0,x;f[0]=-999999999;while (n--){scanf("%s",zfc);ch=zfc[0];if (ch=='I'){scanf("%d",&x);a.push(x);sum+=x;int l=a.size();f[l]=max(sum,f[l-1]);} elseif (ch=='D'&&a.size()>=1){x=a.top();a.pop();sum-=x;} elseif (ch=='L'&&a.size()>=1){x=a.top();a.pop();sum-=x;b.push(x);} elseif (ch=='R'&&b.size()>=1) {x=b.top();b.pop();a.push(x);sum+=x;int l=a.size();f[l]=max(sum,f[l-1]);} else if (ch=='Q'){scanf("%d",&x);printf("%d\n",f[x]);}}}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/YYC-0304/p/9499899.html
總結(jié)
以上是生活随笔為你收集整理的HDOJ 4699-Editor[栈]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CCF OJ 1113-括号匹配[栈]
- 下一篇: 洛谷 P1044 栈 [卡特兰数]