天天看點

UVA540 Team Queue(隊列簡單用法)

隊列:

queue<int> s定義一個隊列

push()入隊

pop()出隊

front()取隊首元素不删除

#include<cstdio>
#include<queue>
#include<map>
using namespace std;
const int maxt = 1000 + 10;
int main()
{
    int t, kase = 0;
    while(scanf("%d", &t) == 1 && t) {
        printf("Scenario #%d\n", ++kase);

        map<int , int> team;//映射作用是編号x對應它的隊伍i
        for(int i = 0; i < t; i++) {
            int n,x;
            scanf("%d", &n);
            while(n--) { scanf("%d", &x); team[x] = i;}
        }

        queue<int> q, q2[maxt];
        //兩個隊列是本題的核心
        //q存放的是隊伍,q2存放的是按增序排列的所有的隊伍以及隊伍下的編号
        //即q存放團隊整體隊列,例{3,1,2}
        //q2存放團隊隊列,例{103,101,102},{201},{301,303}
        for(;;) {
        int x;
        char cmd[10];
        scanf("%s", cmd);
        if(cmd[0] == 'S') break;//遇到STOP停止
        else if(cmd[0] == 'D'){
            int t = q.front();//用變量t表示團隊整體隊列的隊首
            printf("%d\n",q2[t].front()); q2[t].pop();//輸出這個隊首隊伍的第一個人,然後把該人出隊
            if(q2[t].empty()) q.pop();//如果該隊伍在整個隊列中隻有一個人,則q的隊首出隊,即該隊伍出隊
        }else if(cmd[0] == 'E'){
            scanf("%d", &x);
            int t = team[x];//通過map找出x的隊列序号
            if(q2[t].empty()) q.push(t);//如果該隊還沒有人排在隊中,則該隊列插入隊尾
            q2[t].push(x);//把該隊伍的人插入到q2的該隊中
            }
        }
        printf("\n");
    }
    return 0;
}