540 - Team Queue
2016-05-06 19:21
477 查看
Team Queue
PS:因为该题排版较麻烦,这里给出OJ网址:UVa540 - Team Queue有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)。
ENQUEUEx:编号为x的人进入长队。
DEQUEUE:长队的队首出队。
STOP:停止模拟。
对于每个DEQUEUE指令,输出出队的人的编号。
#include <iostream> #include <queue> #include <vector> #include <string> using namespace std; const int maxNum = 1005; // 存放team,一维下标表示组号 vector<int> team[maxNum]; // 获取n所在的team组号,t表示一共多少组 int getTeam(int n, int t) { for(int i = 0; i < t; i++) { for(int j = 0; j < team[i].size(); j++) { if(team[i][j] == n) { return i; } } } // 表示未找到 return -1; } int main() { int kase = 0; // t组队伍 int t; while(cin >> t && t) { cout << "Scenario #" << ++kase << endl; // 编号处理完毕 for(int i = 0; i < t; i++) { // 每组队伍有多少编号 int n; cin >> n; for(int j = 0; j < n; j++) { // 编号 int num; cin >> num; team[i].push_back(num); } } // 操作 string op; // 多少个team,多少个队列 queue<int> teamQueue[maxNum]; // 总队列 queue<int> teams; while(cin >> op) { // STOP 退出 if(op[0] == 'S') { break; } else if(op[0] == 'E') { // ENQUEUE 进队 // 进队编号 int num; cin >> num; // 获得num所在组号 int n = getTeam(num, t); // 该编号未在team容器中 if(n == -1) { continue; } // 如果第n个队列为空,将第n个队列加到总队列中 if(teamQueue .empty()) { teams.push(n); } // 将该编号加入第n个队列 teamQueue .push(num); } else if(op[0] == 'D') { // DEQUEUE 出队 // 找到第一个不为空的队列 int n = teams.front(); cout << teamQueue .front() << endl; teamQueue .pop(); // 队列n全体出列 if(teamQueue .empty()) { teams.pop(); } } } cout << endl; } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge