HDU 1387 Team Queue

题目大意:队列操作,在同一个团队里的可以插队,求最终序列

题解:建立n个子队列,同时建立一个总队列记录团队的先后顺序,插队的直接插到子队列的队尾即可,对总队列没有任何影响。

#include <iostream>   
#include <map> 
#include <string> 
#include <queue> 
using namespace std;  
const int MAX=1010; 
const int INF=99999999;   
map<int,int>Map;  
queue<int>Q[MAX],p;  
string operate;  
bool mark[MAX];  
int main(){  
    int n,m,a,num=0;  
    while(scanf("%d",&n),n){  
        Map.clear();  
        while(!p.empty())p.pop();  
        memset(mark,false,sizeof mark);  
        for(int i=1;i<=n;i++)while(!Q[i].empty())Q[i].pop();  
        for(int i=1;i<=n;i++){  
            scanf("%d",&m);  
            while(m--)scanf("%d",&a),Map[a]=i;  
        }  
        printf("Scenario #%d
",++num);  
        while(cin>>operate,operate!="STOP"){  
            if(operate=="ENQUEUE"){  
                scanf("%d",&a);  
                int id=Map[a];  
                Q[id].push(a);  
                if(!mark[id])p.push(id);  
                mark[id]=true;  
            }else{  
                int k=p.front();  
                printf("%d
",Q[k].front());  
                Q[k].pop();  
                if(Q[k].empty())p.pop(),mark[k]=false;  
            }  
        }  
        cout<<endl;  
    }  
    return 0;  
}

原文地址:https://www.cnblogs.com/forever97/p/3673733.html