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
关于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; }
相关文章推荐
- UVA - 540 Team Queue(STL,队列 )
- UVa - 540 - Team Queue(STL - 队列queue)
- UVA-LA 3135 2004年北京站C题 STL priority_queue自定义优先顺序优先队列的使用
- 5_6 团体队列(UVa540)<queue与STL其他容器的综合运用>
- UVa-136-Ugly Number STL 优先队列的基础使用
- 算法之路二:刘汝佳算法竞赛入门经典:STL队列 团体队列UVA540
- STL中优先级队列的使用priority_queue
- C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)
- 标准模板库STL中优先队列Priority Queues使用手册
- STL 优先队列 priority_queue 的使用
- C++ STL入门教程(4)——stack(栈),queue(队列),priority_queue(优先队列)的使用(附完整程序代码)
- STL中优先队列的使用
- C++ 标准模板库STL 双端队列 deque 使用方法与应用介绍(一)
- STL的队列和栈简单使用
- uva 540 Team Queue(队列+模拟)
- uva540 Team Queue(模拟:队列)
- C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一)
- USACO解析Mixing Milk篇(使用STL优先队列解决)
- STL中的优先队列priority_queue的使用方法
- STL优先队列使用