问题描述
p540相关说明:本题为《算法竞赛入门经典(第2版)》例题 5-6
解法一:多重队列模拟
整个队伍可分为由多个团队分别组成的多个队列 team_queue[num_team] ,每个团队在整个大队列 main_queue 中可以用一个索引(即队伍的编号)来表示。因此解决这题只需要生成队伍编号,然后按照队列的规则进行模拟即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <bits/stdc++.h> using namespace std; int main() { int num_team, num_element, element, num_scenario = 0; while (cin >> num_team && num_team) { cout << "Scenario #" << ++num_scenario << endl; map<int, int> team; for (int i = 0; i < num_team; ++i) { cin >> num_element; while (num_element--) cin >> element, team[element] = i; } queue<int> main_queue, team_queue[num_team]; string command; while (cin >> command && command != "STOP") { if (command == "DEQUEUE") { int front_team = main_queue.front(); cout << team_queue[front_team].front() << endl; team_queue[front_team].pop(); if (team_queue[front_team].empty()) main_queue.pop(); } else if (command == "ENQUEUE") { cin >> element; int cur_team = team[element]; if (team_queue[cur_team].empty()) main_queue.push(cur_team); team_queue[cur_team].push(element); } } cout << endl; } return 0; } |
1 |
#TODO |