《算法竞赛进阶指南》0x12 队列 POJ2259 Team Queue

题目链接:http://poj.org/problem?id=2259

由于同一个队伍的一定是连续的排队的,所以用一个队列记录排队的队伍顺序,用N个记录每个队伍内部的顺序。

代码:

#include<iostream>
#include<queue>
using namespace std;
#define maxn 1006
queue<int> q[maxn];
int t,n;
int f[1000010];    
int id=0;
void work(){
    for(int i=0;i<maxn;i++)while(q[i].size())q[i].pop();

    for(int i=1;i<=t;i++){
        int k;
        scanf("%d",&n);
        while(n--){
            scanf("%d",&k);
            f[k]=i;//所属的队伍 
        }
    }
    cout<<"Scenario #"<<++id<<endl;
    char s[20];
    while(scanf("%s",s)){
        if(s[0]=='S')break;
        else if(s[0]=='E'){
            int x;
            scanf("%d",&x);
            //队列中没有这个队伍的人,放在队列最后 
            if(q[f[x]].size()==0)q[0].push(f[x]),q[f[x]].push(x);
            else q[f[x]].push(x); 
        }
        else if(s[0]=='D'){
            if(q[0].size()){
                printf("%d
",q[q[0].front()].front());
                q[q[0].front()].pop();
                if(q[q[0].front()].empty())q[0].pop();
            }
        }
    }
    cout<<endl;
    return ;
}
int main(){
    while(cin>>t && t){
        work();
    }
} 
原文地址:https://www.cnblogs.com/randy-lo/p/13151131.html