您的位置:首页 > 其它

UVA540 STL中队列的使用

2017-04-09 14:24 357 查看
  这题让我们熟悉了STL中队列的使用,和上一篇博客中提到的集合的嵌套类似,这个题目中设计到了队列的嵌套,所以可以用map容器给不同的队列映射一个ID来区分。

关于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 (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

// UVA540.cpp : 定义控制台应用程序的入口点。
//

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
const int max_id = 1005; // 题目描述最多的团体数量
int main()
{
int num=0;//测试用例个数
int casenum = 0;
while (cin >> num&&num != 0)
{
cout << "Scenario #" << ++casenum<<endl;
map<unsigned int, unsigned int>q_team_id;//用来存储各个团体的编号,用来进行大队列的编号和团体的映射
for (int i = 0; i < num;i++)
{
int n = 0;
cin >> n;
for (int j = 0; j < n; j++)
{
unsigned int x = 0;
cin >> x;
q_team_id[x] = i; //这样使数组序号与团队的编号是对应的
}
}
queue<unsigned int> q_id_queue;//团体编号形成的大队列
queue<unsigned int> q_id_queue_elment[max_id];//上述大队列中编号对应的具体的成员存储在这里
//数组序号与团队的编号是对应的
while (1)
{
char cmd[15] = { 0 };
cin >> cmd;
if (cmd[0] == 'S') break;
else if (cmd[0] == 'E'){
unsigned int temp = 0;
cin >> temp;
unsigned int x = q_team_id[temp];
if (q_id_queue_elment[x].empty())q_id_queue.push(x);//如果在大队列中没有这个团体,则加入这个团体

q_id_queue_elment[x].push(temp); //团体中的具体元素入对应的团体中
}
else if (cmd[0] == 'D'){
unsigned int x = q_id_queue.front();
cout << q_id_queue_elment[x].front() << endl;
q_id_queue_elment[x].pop();
if (q_id_queue_elment[x].empty())
q_id_queue.pop();
}
}
cout << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stl