Team Queue
Descriptions:
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 file 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
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.
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
先说一下题目要求
有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友身后。如果没有任何一个队友排队,则他会排到长队的队尾。输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)。 ENQUEUE:编号为X的人进入长队。 DEQUEUE:长队队首出队。 STOP:停止模拟。 对于每个DEQUEUE指令,输出出队的人的编号。
输入
输入文件中有一组或多组测试数据。 每组测试数据开始有t个团队。下面t行,每行的第一个数字代表这个团队人数,后面是这几个人的编号。编号为0到999999之间的一个整数。 每组测试数据以“STOP”结束。 输入以t==0时结束。 警告:一个测试用例可能包含最多200000(二十万)个命令,所以实现 团队的队列应该是有效的。
输出
对于每组测试数据,先打印一句"Scenario #k",k是第几组数据。对于每一个"DEQUEUE"指令,输出一个出队的人的编号。每组测试数据后要换行,即使是最后一组测试数据。
题目链接:
https://vjudge.net/problem/UVA-540
先用map找到每个人员所对应的团队,再用队列queue去分别存放团队的编号和人员的编号
AC代码
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <sstream> #define mod 1000000007 #define ll long long #define INF 0x3f3f3f3f #define ME0(x) memset(x,0,sizeof(x)) using namespace std; int main() { int n; int sum=0; while(cin>>n,n) { sum++; cout<<"Scenario #"<<sum<<endl; map<int,int> team;//一个人员编号对应一个团队 for(int i=0; i<n; i++) { int x; cin>>x; for(int j=0; j<x; j++) { int num; cin>>num; team[num]=i;//人员编号对应他的团队序号 } } string s; queue<int> big;//团队的队列,代表有几个团队 queue<int> small[10005];//每个团队人员的队列 while(cin>>s&&s!="STOP") { if(s=="ENQUEUE") { int num; cin>>num; int t=team[num];//这到这个人所对应的团队 if(small[t].empty())//如果找不到团队,则重新建立一个团队 big.push(t); small[t].push(num);//把这个人让如这个团队 } if(s=="DEQUEUE") { int t=big.front();//取出第一个团队 if(!small[t].empty())//团队不为空,则去取这个团队的人员 { cout<<small[t].front()<<endl; small[t].pop(); } if(small[t].empty()) big.pop(); } } cout<<endl; } }