uva 540(Team Queue)

来源:互联网 发布:仅限数据连接怎么取消 编辑:程序博客网 时间:2024/06/07 00:30

题目大意:

   有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。

   输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)


对于每个DEQUEUE指令,输出出队人的编号。


题目分析:

   没有插队的时候,就是普通的队列问题,入队出队就可以了。有了插队应该怎么处理呢,把团队看成一个队列,如果有成员进来,加入到所属团队的队列。那么整个队列就变成了队列的队列;


参考代码:

#include <cstdio>#include <cstring>#include <map>#include <queue>using namespace std;map<int, int> team; //team[x] 表示编号为x的人所在的团队;queue<int> q, q2[1010];  //q队列中存放团队队列的编号; q2[i]表示第i个队伍的队列;int inqueue[1010];int main(){    int cas = 0;    int t;  // t个团队;    while(scanf("%d", &t) != EOF && t){        int n, x;        for(int i = 0; i < t; ++i){            scanf("%d", &n);            for(int j = 0; j < n; ++j){                scanf("%d", &x);                team[x] = i;            }        }        char op[10];        memset(inqueue, 0, sizeof(inqueue));        printf("Scenario #%d\n", ++cas);        while(scanf("%s", op) && op[0] != 'S'){            if(op[0] == 'E'){                scanf("%d", &x);                if(!inqueue[team[x]]){   //判断这个人所在的团队有没有在队列中                    q.push(team[x]);                    inqueue[team[x]] = 1;                }                q2[team[x]].push(x);            } else if(op[0] == 'D'){                int outx;         // outx表示出队的人;                int t = q.front(); // 获得队首的队列;                while(q2[t].empty()){                    q.pop();                    inqueue[t] = 0;                    t = q.front();                }                outx = q2[t].front();                q2[t].pop();                printf("%d\n", outx);            }        }        printf("\n");        while(!q.empty())            q.pop();        for(int i = 0; i < t; ++i)            while(!q2[i].empty())                q2[i].pop();    }}




0 0