[UVA540]Team Queue(模拟 + 队列)

本文介绍了一种使用队列和映射实现的任务分配算法。通过将一个大任务队列拆分为多个小队列,并利用成员到小队列的映射来高效地进行任务分配和检索。该算法特别适用于模拟插队等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

把一个大的队列分成几个小队列,大队列里放的是队列的编号,小队列里放的是成员的编码。进行ENQUEUE操作时,要看大队列中是否有这个成员所在小队列即该小队列是否为空。若为空则向大队列push小队列编号(并向小队列push该成员),否则直接向小队列push该成员(模拟插队)。
注:用map构建成员到team编号的映射。

#include <map>
#include <iostream>
#include <queue>
#include <sstream>
using namespace std;

const int maxn = 1000 + 5;

map<int, int> team;     // 成员 to Team

int main() {
    //freopen("input.txt", "r", stdin);
    int T;
    int kase = 0;
    while(scanf("%d", &T) != EOF && T) {
        cout << "Scenario #" << ++kase << endl;
        queue<int> qu, quteam[maxn];      
        team.clear();
        for(int i = 0; i < T; i++) {
            int n;
            cin >> n;
            for(int j = 0; j < n; j++) {
                int t;
                cin >> t;
                team[t] = i;
            }
        }
        string op;
        while(cin >> op) {
            if (op[0] == 'S')  break;
            else if(op[0] == 'E') {
                int mem;
                cin >> mem;
                int t = team[mem];
                if (quteam[t].empty())  qu.push(t);
                quteam[t].push(mem);
            }
            else {
                int t = qu.front();
                cout << quteam[t].front() << endl;
                quteam[t].pop();
                if (quteam[t].empty()) qu.pop();
            }
        }
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值