HDU4699——Editor

Problem

Editor


Editor-Problem.png

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:


Editor-Hint.png

思路

手写一个可遍历的栈,然后模拟操作~~

代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 1e6 + 5;
int a[M];
class Stack {
private:
    int size;
    int a[M];
public:
    Stack() :size(0) { memset(a, 0, sizeof(a)); }
    void init() { size = 0; memset(a, 0, sizeof(a)); }
    bool empty() { return size == 0; }
    void push(int x) { a[size++] = x; }
    void pop() { --size; }
    int top() { return a[size - 1]; }
    int get(int k) { return a[k - 1]; }
};
Stack L, R;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int Q;
    char c;
    while (cin >> Q) {
        memset(a, 0, sizeof(a));
        L.init(); R.init();
        int step = 0, sum = 0;
        while (Q--) {
            cin >> c;
            if (c == 'I') {
                cin >> a[step];
                sum += a[step++];
                if (L.empty())L.push(sum);
                else L.push(max(sum, L.top()));
            }
            else if (c == 'D' && !L.empty()) {
                sum -= a[--step];
                L.pop();
            }
            else if (c == 'L' && !L.empty()) {
                R.push(a[--step]);
                sum -= a[step];
                L.pop();
            }
            else if (c == 'R' && !R.empty()) {
                a[step] = R.top();
                R.pop();
                sum += a[step++];
                if (L.empty())L.push(sum);
                else L.push(max(sum, L.top()));
            }
            else if (c == 'Q') {
                int k;
                cin >> k;
                cout << L.get(k) << endl;
            }
        }
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 医院 “白医生,下班啦!”一位女护士亲切的问到。 “是啊,辛苦啦!我先走了。”白衣冲她挥了挥手便走出了医院大门。 ...
    白衣素装阅读 638评论 2 8
  • 情绪总是被一些小事击溃
    WYqqqqqQ阅读 312评论 0 0
  • 轻快遨游大海的鱼儿,只有短短7秒钟的记忆,我们也曾羡慕过,在记忆很痛的时候,可若让所有记忆消失,难道不会恐惧...
    娅梦忆轩阅读 299评论 0 1
  • 吃下勇者的坚果饼 是不是就可以变得刀枪不入 如果真有这么好的事 每个人都可以不用担心皱纹的事情 从午夜大笑到天明 ...
    山屈生阅读 308评论 0 1