Editor
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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 <stdio.h> #include <string.h> #include <string> #include <map> #include <vector> #include <set> #include <algorithm> #include <vector> #include <stack> #include <math.h> #include <stdlib.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; typedef long long LL ; const int size=1000008 ; int sum[size] ; int Left[size] ; int Right[size] ; int dp[size] ; struct Me{ int N ; int left_top ; int right_top ; Me(){} ; Me(int n):N(n){ left_top=0 ; right_top=0 ; sum[0]=0 ; fill(dp,dp+1+N,-10000) ; } void gao(){ char str[2] ; int x ; for(int i=1;i<=N;i++){ scanf("%s",str) ; if(str[0]=='I'){ scanf("%d",&x) ; Left[++left_top]=x ; sum[left_top]=sum[left_top-1]+x ; dp[left_top]=Max(dp[left_top-1],sum[left_top]) ; } else if(str[0]=='D'){ if(left_top) left_top-- ; } else if(str[0]=='L'){ if(left_top){ Right[++right_top]=Left[left_top] ; left_top-- ; } } else if(str[0]=='R'){ if(right_top){ Left[++left_top]=Right[right_top] ; right_top-- ; sum[left_top]=sum[left_top-1]+Left[left_top] ; dp[left_top]=Max(dp[left_top-1],sum[left_top]) ; } } else{ scanf("%d",&x) ; printf("%d ",dp[Min(left_top,x)]) ; } } } }; int main(){ int n ; while(scanf("%d",&n)!=EOF){ Me me(n); me.gao() ; } return 0 ; }