Poj2259 Team Queue 【队列】

http://poj.org/problem?id=2259

队列是一种先进先出的数据结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称作队尾,进行删除操作的前段称作队首。队列中没有元素时,称作空队列。

题目大意:在组队队列中,每个元素(element)属于一支队伍。如果一个元素将要进入组队队列时,它会先从头到尾先检查他的队友(同属一支队伍)是否已经在队列中,如果找到了,他就会紧随其后进入队伍。如果没有找到,则他会从队列末尾入队,并成为自己队伍的第一个元素。出队操作则跟普通队列一样:元素在组队队列中按从头到尾的顺序出列。

假设现在有n个队伍,就开n个队列,其中q[i]表示第i个队列;另外再开一个队列qq记录team前后的信息。

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
queue <int> q[1010];
queue <int> qq;
char od[22];
int belong[1000100];
int cntq[1010];
int main() {
    int n , cas = 1;
    while(~scanf("%d",&n) && n) {
        printf("Scenario #%d\n" , cas ++);
        for(int i=0;i<n;i++) while(!q[i].empty()) q[i].pop();
        while(!qq.empty()) qq.pop();
        for(int i=0;i<n;i++) {
            int m;
            scanf("%d",&m);
            while(m --) {
                int a;
                scanf("%d",&a);
                belong[a] = i;
            }
            cntq[i] = 0;
        }
        while(scanf("%s",od) && od[0] != 'S') {
            if(od[0] == 'E') {
                int a , b;
                scanf("%d",&a);
                b = belong[a];
                q[b].push(a);
                if(cntq[b] == 0) qq.push(b);
                cntq[b] ++;
            }
            else {
                int a = qq.front();
                cntq[a] --;
                if(cntq[a] == 0) qq.pop();
                int b = q[a].front();
                printf("%d\n",b);
                q[a].pop();
            }
        }
        /*
        while(!qq.empty()) {
            int a = qq.front();
            qq.pop();
            int b = q[a].front();
            q[a].pop();
        }
        */
        puts("");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/aiiYuu/archive/2013/04/07/3003311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值