540 - Team Queue

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[n].empty()) {
                    teams.push(n);
                }
                // 将该编号加入第n个队列
                teamQueue[n].push(num);
            } else if(op[0] == ‘D‘) {
                // DEQUEUE 出队
                // 找到第一个不为空的队列
                int n = teams.front();
                cout << teamQueue[n].front() << endl;
                teamQueue[n].pop();
                // 队列n全体出列
                if(teamQueue[n].empty()) {
                    teams.pop();
                }
            }
        }
        cout << endl;
    }

    return 0;
}
时间: 2024-10-11 14:47:36

540 - Team Queue的相关文章

UVa 540 Team Queue(团队队列)

题意  模拟团队队列的入队和出队 STL应用  用一个队列维护团队编号  再用一个队列数组维护个体 #include <cstdio> #include <cstring> #include <queue> #include <map> using namespace std; const int N = 1000005; int team[N]; int main() { int cas = 0, n, t, a; char cmd[20]; while(

UVA - 540 Team Queue(STL,队列 )

Team Queue Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known,

Uva 540.Team Queue

队列问题,思路较为清晰 通过模拟操作可以发现可以先队内排列,然后进行队伍排列 其中个别操作由于vector.map嵌套,可能会发生打错凌乱的情况. 1 #include <cstdio> 2 #include <vector> 3 #include <queue> 4 #include <map> 5 #include <algorithm> 6 7 using namespace std; 8 9 int kase=0; 10 11 class

【UVa 540】Team Queue

  Team 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

Team Queue

Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 129 Accepted Submission(s): 63   Problem Description Queues and Priority Queues are data structures which are known to most computer scie

Winter-2-STL-G Team Queue 解题报告及测试数据

Time Limit:3000MS     Memory Limit:0KB Description 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 q

HDU 1387 Team Queue

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1889    Accepted Submission(s): 639 Problem Description Queues and Priority Queues are data structures which are known to most computer

HDU 1387 Team Queue( 单向链表 )

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1294    Accepted Submission(s): 442 Problem Description Queues and Priority Queues are data structures which are known to most computer

hdu 1387(Team Queue) STL

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2051    Accepted Submission(s): 713 Problem Description Queues and Priority Queues are data structures which are known to most computer