Team Queue(队列)

题目链接

题目描述:

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)(1 \leq t \leq1000)(1t1000). 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. 
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
 
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.
 
Example:
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

output:

Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001

 题意:多实例输入,每次给定t个团体,每个团体有n个人,每个人有不同的编号。这些团体中的人们开始排队,如果当前要排队的人,在前面的队伍中有和他一个团体的人,那么这个人就排在他的团体的成员的最后;否则就排在整个队伍的最后。多次操作,每次操作插入人或者输出队伍第一个人的编号。

思路:用stl中的queue、map进行操作,qx记录当前队伍的第一个团体的编号,q[i]记录编号为i的团体的成员。

#include<bits/stdc++.h>
#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef unsigned long long ull;
typedef  long long ll;
typedef pair<ll,ll> pi;
#define IOS std::ios::sync_with_stdio(false)
#define ls p<<1
#define rs p<<1|1
#define mod 1000000000 + 7
#define PI acos(-1.0)
#define INF   1e9
#define N 200000 + 5
/*********************Code*********************/
ll t,Case,n,m,x;
queue<ll>q[2021],qx;
map<ll,ll>mp;
string s;
int main(void){
    IOS;
    while(cin>>t){
        if(!t)
            break;
            Case++;
            for(ll i = 0;i < 2021;i++){
                while(!q[i].empty())
                    q[i].pop();
            }
            while(!qx.empty())
                qx.pop();
            mp.clear();
        for(ll i = 1;i <=t;i++){
            cin>>n;
            for(ll j = 1;j <=n;j++){
                cin>>x;
                mp[x] = i;
            }
        }
        cout<<"Scenario #"<<Case<<endl;
        while(cin>>s){
            if(s=="STOP")
                break;
            if(s=="ENQUEUE"){
                    cin>>x;
            ll k = mp[x];
            if(q[k].empty()){
                qx.push(k);
            }
            q[k].push(x);
            }
            else{
                if(q[qx.front()].size()==1){  //如果当前团体的成员只剩下一个,要删除该团体的编号。
                    cout<<q[qx.front()].front()<<endl;
                    q[qx.front()].pop();
                    qx.pop();
                }
                else{
                    cout<<q[qx.front()].front()<<endl;
                    q[qx.front()].pop();
                }
            }
        }
        cout<<endl;
    }
}

 

 
posted @ 2021-02-17 16:06  阿涅—Rachel  阅读(85)  评论(0)    收藏  举报