UVa 540 - Team Queue

本文介绍了一种使用队列数据结构来模拟团队管理的方法。通过定义固定大小的队列来跟踪不同团队成员的状态,实现了成员加入和离开团队的操作。此外,还提供了完整的C++实现代码,展示了如何有效地管理和更新团队成员的状态。

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

/*
队列操作, 用了一个1000000的数组记录每个人所属team
*/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <queue>
using namespace std;

const int MAXT = 1004;
const int HASH_SIZE = 1000004;
const int BUFF_SIZE = 16;
int vis[HASH_SIZE];
char buff[BUFF_SIZE];
queue<int> q[MAXT];
queue<int> teams;
int t;

void init()
{
    while(!teams.empty()) teams.pop();
    for(int i=1; i<=t; i++) {
        while(!q[i].empty()) q[i].pop();
    }
}

void inq(int n)
{
    int i = vis[n];
    if(q[i].size() == 0) {
        teams.push(i);
    }
    q[i].push(n);
}

int outq()
{
    int i = teams.front();
    int n = q[i].front();
    q[i].pop();
    if(q[i].size() == 0) {
        teams.pop();
    }
    return n;
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    int n, k, T=0;
    while(scanf("%d", &t) == 1 && t) {
        for(int i=1; i<=t; i++) {
            scanf("%d", &k);
            for(int j=0; j<k; j++) {
                scanf("%d", &n);
                vis[n] = i;
            }
        }
        printf("Scenario #%d\n", ++T);
        init();
        while(scanf("%s", buff)) {
            if(buff[0] == 'S') break;
            if(buff[0] == 'E') {
                scanf("%d", &n);
                inq(n);
            } else {
                printf("%d\n", outq());
            }
        }
        printf("\n");

    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值