1. 程式人生 > 其它 >HDU 7072-Boring data structure problem

HDU 7072-Boring data structure problem

https://acm.hdu.edu.cn/showproblem.php?pid=7072

deque 模擬下每次加入數刪除數即可,記錄數是否被刪除,數在左que還是右que,que中沒被刪除的數的個數


const int maxn = 2e7 + 7;

int n, t, m, x, sizl, sizr;
bool vis[maxn], pls[maxn];

struct node {
    int num[maxn], l = maxn / 2, r = maxn / 2 - 1;

    bool empty() const {
        return r < l;
    }

    void push_back(int x) {
        num[++r] = x;
    }

    void push_front(int x) {
        num[--l] = x;
    }

    int front() {
        return num[l];
    }

    int back() {
        return num[r];
    }

    void pop_back() {
        r--;
    }

    void pop_front() {
        l++;
    }
} lq, rq;

char q;

void update() {
    again:
    int tot = sizr + sizl;

    while (!lq.empty() && vis[lq.back()]) lq.pop_back();
    while (!rq.empty() && vis[rq.front()]) rq.pop_front();

    if (sizl == tot / 2 + 1 || tot == 0) return;
    if (sizl > tot / 2 + 1) {
        while (!lq.empty() && vis[lq.back()]) lq.pop_back();
        if (!lq.empty()) {
            rq.push_front(lq.back());
            lq.pop_back();
            pls[rq.front()] = 0;
            sizr++, sizl--;
        }
    } else {
        while (!rq.empty() && vis[rq.front()]) rq.pop_front();
        if (!rq.empty()) {
            lq.push_back(rq.front());
            rq.pop_front();
            pls[lq.back()] = 1;
            sizr--, sizl++;
        }
    }
    goto again;
}

void solve() {
    scanf("%d", &n);
    for (int i = 1, pl = 1; i <= n; i++) {
        scanf(" %c", &q);
        if (q == 'Q') {
            update();
            printf("%d\n", lq.back());
        } else {
            if (q == 'L') {
                lq.push_front(pl);
                pls[pl++] = 1, sizl++;
            } else if (q == 'R') {
                rq.push_back(pl);
                pls[pl++] = 0, sizr++;
            } else {
                scanf("%d", &x);
                if (pls[x] == 1) sizl--, vis[x] = 1;
                else sizr--, vis[x] = 1;
            }
            update();
        }
    }
我看見 你