Uva 540- 团体队列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 file will contain one or more test cases. Each test case begins with the number of teams t (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. 
Sample Input 

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 

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 

Sample Output 
Scenario #1 
101 
102 
103 
201 
202 
203

Scenario #2 
259001 
259002 
259003 
259004 
259005 
260001

 

 

分析

1.主要考察的还是stl容器的应用,难度不高,一定要耐下心来,慢慢理解...切忌浮躁,一瞬间理解不了,看到遍数多了sense也就来了,不要轻易放弃,不要轻易问别人。

2.queue的成员函数(先进先出)

  • q.empty()判断队列q是否为空,当队列q空时,返回true;否则为false(值为0(false)/1(true))。
  • q.size()访问队列q中的元素个数。(不可写成sizeof(q)或size(q))
  • q.push(a)会将一个元素a置入队列q中。
  • q.front()会返回队列q内的第一个元素(也就是第一个被置入的元素)。(不可写成front(q))
  • q.back()会返回队列q中最后一个元素(也就是最后被插入的元素)。(不可写成back(q))
  • q.pop()会从队列q中移除第一个元素。(不可写成pop(q)) [1]  
    注意:pop()虽然会移除下一个元素,但是并不返回它。
    front()和back()返回下一个元素但并不移除该元素。
    在stack库中的函数与queue很类似,但是stack中要返回元素时,只能返回最后一个元素,且函数名不一样(stack中为s.top()),需要区分。

 

代码

 1 #include<iostream>
 2 #include<map>
 3 #include<queue>
 4 using namespace std; 
 5 const int maxt=1000+10;
 6 int main()
 7 {
 8     int t,flag=0;
 9     while(cin>>t&&t)
10     {
11         printf("Scenario #%d\n", ++flag);
12         
13         //记录所有人的团队编号 
14         map<int,int>team;       //表示编号为x的人所在的团队编号 
15         for(int i=0;i<t;i++)    //i指的就是团队编号  
16         {
17             int n,x;
18             cin>>n;
19             while(n--)        //每个团i里面有n个人 
20             {
21                 cin>>x;
22                 team[x]=i;    //每个人的编号为x,在i团里。放入map表中 
23             }
24         }
25         
26         //模拟 
27         queue<int>q,q2[maxt];//q是团队的队列,q2[i]是团队i成员的队列 
28         for(;;)
29         {
30             int x;
31             char cmd[10];
32             cin>>cmd;
33             if(cmd[0]=='S')break;
34             
35             else if(cmd[0]=='D')
36             {
37                 int t=q.front();//如整体团队队列为{3,1,2} ,t为团队2 
38                 cout<<q2[t].front()<<endl;   //输出团队2中的最前列队员编号 
39                 q2[t].pop();
40                 if(q2[t].empty())q.pop();  //团队2中的成员为空,释放团队队列中的团队2 
41             }
42             
43             //添加队列 
44             else if(cmd[0]=='E')
45             {
46                 cin>>x;
47                 if(q2[team[x]].empty()) q.push(team[x]);
48                 q2[team[x]].push(x);
49             }
50         }
51         cout<<endl;
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/masking-timeflows/p/6645267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值