Team Queue

Posted 勿忘初心0924

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Team Queue相关的知识,希望对你有一定的参考价值。

Team Queue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 129 Accepted Submission(s): 63
 
Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.

Your task is to write a program that simulates such a team queue.

 
Input
The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.

Finally, a list of commands follows. There are three different kinds of commands:

ENQUEUE x - enter element x into the team queue
DEQUEUE - process the first element and remove it from the queue
STOP - end of test case
The input will be terminated by a value of 0 for t.

 
Output
For each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.
 
Sample Input
2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE 102
ENQUEUE 202
ENQUEUE 103
ENQUEUE 203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005 260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
 
Sample Output
Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001
 
 
Source
University of Ulm Local Contest 1998
 
Recommend
Eddy
/*
题意:模拟队列,初始的情况有两个队列,现在将两个队列中去元素进行添加到一个新的队列中,
    两种操作ENQUEUE a:添加元素a进入队列,如果队列中有它的“队友”就是原来和他在一个队的,就可以插到她的后边
            DEQUEUE  :队列的元素头的元素出队列
初步思路:用queue进行模拟,建n个队列嘛,实际上如果队列中只要有一个某一个原始队列中的元素,那么从这个数开始
    ,后边几个全是这个原始队列中的数,至于查询的时候可以用map进行查找

*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1001;
bool visited[maxn];
void init(){
    memset(visited,false,sizeof(visited));
}
int main(){
    //freopen("in.txt","r",stdin);
    int cnt = 1;
    int n;
    while(scanf("%d",&n)!=EOF,n){
        init();
        queue<int> q[maxn];//每个小队.存储的是每个个体的编号
        queue<int> que;//总队列。存储的是每个小队的编号
        map<int,int> team;//team[t] = i 。表示的是编号为t的人属于第i个小队
        for(int i=0;i<n;++i){
            int m;
            scanf("%d",&m);
            while(m--){
                int teamnums;
                scanf("%d",&teamnums);
                team[teamnums] = i;//为每个编号的人初始化小队...
            }
        }
        printf("Scenario #%d\n",cnt++);
        string str;
        while(cin >> str){
            if(str == "STOP"){
                break;
            }else if(str == "ENQUEUE"){//如果当前指令是ENQUEUE
                int t;
                scanf("%d",&t);
                q[team[t]].push(t);  //将t插入到它所对应的小队中
                
                if(visited[team[t]] == false){  //如果该小队还没有被访问过(还没有在总队列中)
                    
                    que.push(team[t]);//将该小队的序号插入到总队列中
                    visited[team[t]] = true;//将该小队标记为已经访问过
                }
            }else if(str == "DEQUEUE"){//如果当前指令是DEQUEUE
            
                printf("%d\n",q[que.front()].front());//那么打印排在总队列排在队首的小队中排在队首的元素
                q[que.front()].pop();//将该小队中对手的元素出队

                if(q[que.front()].empty() == true){//如果该小队已经为空
                
                    visited[que.front()] = false;//那么将该小队重新标记为为访问过
                    que.pop();//将该小队的编号从总队列中出队..
                }
            }
        }

        printf("\n");
    }

    return 0;
}

 

以上是关于Team Queue的主要内容,如果未能解决你的问题,请参考以下文章

UVA 540 Team Queue

Team Queue

Team Queue POJ - 2259 (队列)

Team Queue

Week 1 # F Team Queue

hdu 1387(Team Queue) STL