Team Queue(POJ 2259)

题意:有若干个团体,每个团体有若干个元素,他们按次序来排队,如果队列中已经有同一团体的元素在,则可以插队到它后面,模拟这个过程

思路:用map存下元素与团体的关系,并开2个队列,一个存整体队伍的排列(毕竟同一个团体的元素会连在一起),另一个存每个团体内部的排列。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std; 
int read(){
    char ch=getchar();int f=1,t=0;
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
    return t*f;
}
int main(){
    int n,T=0;
    while (scanf("%d",&n)!=EOF&&n!=0){
        printf("Scenario #%d
",++T); 
        map<int,int>mp;
        for (int i=1;i<=n;i++){
            int x=read();
            while (x--){
                mp[read()]=i;
            }
        }
        queue<int> qAll,qTeam[2005];
        char s[50];
        scanf("%s",s);
        while (s[0]!='S'){
            if (s[0]=='E'){
                int x=read();
                int y=mp[x];
                if (qTeam[y].empty()) qAll.push(y);
                qTeam[y].push(x); 
            }else if (s[0]=='D'){
                int x=qAll.front();
                printf("%d
",qTeam[x].front());qTeam[x].pop();
                if (qTeam[x].empty()) qAll.pop();
            }
            scanf("%s",s);
        }
        printf("
");
    }
}
原文地址:https://www.cnblogs.com/qzqzgfy/p/5266789.html