AcWing 132. 小組隊列 (雙端隊列運用)

傳送門

用一個隊列來存當前隊列裏隊伍的先後順序,然後再用一個隊列來存每個隊伍里人的先後順序,真的是很奇妙呢。

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e6 + 10;
deque<int> P[1010], Q;
int t, n, tmp, Group[MAXN];
string s;

int main() {
    //freopen("in", "r", stdin);
    ios::sync_with_stdio(false);
    int Case = 0;
    while (cin >> t && t) {
        for (int i = 0; i < t; i++) {
            P[i].clear();
            Group[i] = 0;
        }
        Q.clear();
        for (int Cas = 1; Cas <= t; Cas++) {
            cin >> n;
            for (int i = 1; i <= n; i++) {
                cin >> tmp;
                Group[tmp] = Cas;
            }
        }
        cout << "Scenario #" << ++Case << endl;
        while (cin >> s) {
            if (s[0] == 'S')
                break;
            if (s[0] == 'E') {
                cin >> tmp;
                if (!P[Group[tmp]].size())
                    Q.push_back(Group[tmp]);
                P[Group[tmp]].push_back(tmp);
            } else if (s[0] == 'D') {
                int top = Q.front();
                cout << P[top].front() << endl;
                P[top].pop_front();
                if (!P[top].size())
                    Q.pop_front();
            }
        }
        cout << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章