HDU 4699 Editor

题目:

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1000005;

int sum[N] = {};
int f[N] = {};

stack<int> A,B;

int main(){

std::ios::sync_with_stdio(false);
cin.tie(NULL);

char c;
int t;
f[0] = -INF;
sum[0] = 0;

int tmp = 0;
cin >> tmp;

while(tmp--){
cin >> c;
if(c == 'I'){
cin >> t;
A.push(t);
sum[A.size()] = sum[A.size() - 1] + A.top();
f[A.size()] = max(f[A.size() - 1], sum[A.size()]);
}
if(c == 'D'){
if(!A.empty())
A.pop();
}
if(c == 'L'){
if(!A.empty())
{
B.push(A.top());
A.pop();
}
}
if(c == 'R'){
if(!B.empty())
{
A.push(B.top());
B.pop();
sum[A.size()] = sum[A.size() - 1] + A.top();
f[A.size()] = max(f[A.size() - 1], sum[A.size()]);
}
}
if(c == 'Q'){
cin >> t;
cout << f[t] << endl;
}
}
}

Debug

未考虑栈空情况,导致段错误,添加if判断即可。