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

思路

手寫一個(gè)可遍歷的棧,然后模擬操作~~

代碼

#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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 醫(yī)院 “白醫(yī)生,下班啦!”一位女護(hù)士親切的問(wèn)到。 “是啊,辛苦啦!我先走了?!卑滓聸_她揮了揮手便走出了醫(yī)院大門。 ...
    白衣素裝閱讀 730評(píng)論 2 8
  • 情緒總是被一些小事?lián)魸?/div>
    WYqqqqqQ閱讀 350評(píng)論 0 0
  • 輕快遨游大海的魚兒,只有短短7秒鐘的記憶,我們也曾羨慕過(guò),在記憶很痛的時(shí)候,可若讓所有記憶消失,難道不會(huì)恐懼...
    婭夢(mèng)憶軒閱讀 362評(píng)論 0 1
  • 吃下勇者的堅(jiān)果餅 是不是就可以變得刀槍不入 如果真有這么好的事 每個(gè)人都可以不用擔(dān)心皺紋的事情 從午夜大笑到天明 ...
    山屈生閱讀 329評(píng)論 0 1

友情鏈接更多精彩內(nèi)容