团队队列(列和map结合的经典运用)

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

Sample Output

Scenario #1

101 102 103 201 202 203

意思就是很多团队相互排队,若你的团队中有人在队伍中,就可以插到你团队的最后面否则就只能插到队伍的最后面!!

这题map的使用和俩个queue队列使用时关键,map<int ,int >team有效的将成员和团队编号链接了起来,就可以进行t=team[x]的对应了。而queue <int >q,q2[maxn]代表俩个队伍,一个为总队伍中的团队编号队伍,二是队伍中的团队小队伍,

接下来照着题目操作即可

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<set>
 6 #include<map>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 #include<algorithm>
11 #include<iostream>
12 #include<cstdio>
13 #include<algorithm>
14 using namespace std;
15 const int maxn=1005;
16 int main()
17 {
18 
19   int n;
20   int fl=1;
21   while(cin>>n)
22   {
23 
24       int flag=1;
25       map <int,int> team;
26       if(n==0)  break;
27       cout<<"Scenario #"<<fl++<<endl;
28       while(n--)
29       {
30           int t,x;
31           cin>>t;
32           for(int i=0;i<t;i++)
33              {
34                  cin>>x;
35                  team[x]=flag;
36              }
37             flag++;
38         }
39         queue<int>q,q2[maxn];
40         for(;;)
41         {
42             int x;
43             char cmd[10];
44             cin>>cmd;
45             if(cmd[0]=='S') break;
46             else if(cmd[0]=='D')
47             {
48                 int t=q.front();
49                 cout<<q2[t].front()<<endl;
50                 q2[t].pop();
51                 if(q2[t].empty()) q.pop();
52             }
53             else if(cmd[0]=='E')
54             {
55 
56                 cin>>x;
57                 int t=team[x];
58                 if(q2[t].empty()) q.push(t);
59                 q2[t].push(x);
60             }
61 
62 }
63         cout<<endl;
64 }
65     return 0;
66 }

原文地址:https://www.cnblogs.com/blvt/p/7202606.html