继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

团体队列 UVA540 Team Queue

米脂
关注TA
已关注
手记 492
粉丝 88
获赞 590

一个长队。每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会被排到长队的队尾。

输入每个团队中所有队员的编号,要求支持如下3中指令:

ENQUEUE x:编号为x的人进入长队

DEQUEUE:长队的队首出队

STOP:停止模拟

对于每个DEQUEUE指令,输出出队的人的编号

样例输入

2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE
102
ENQUEUE 202
ENQUEUE 103
ENQUEUE
203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5
259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005
260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE
259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE
260002
ENQUEUE
260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0

样例输出

Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001

#define LOCAL#include<iostream>#include<cstdio>#include<map>
#include<queue>#include<string>using namespace std;const int maxn=100;int main(){    
#ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);    
    #endif

    //!1.记录所有人的团队编号,从0开始
    int t;//共有t个团队
    int kase=0;    while(scanf("%d",&t) == 1 && t>0){       
     map<int,int> team;        
     cout<<"Scenario #"<<++kase<<endl;        
     for(int i=0;i<t;i++){//第i个团队
            //有n个人
            int n;            
            cin>>n;           
             int code;           
              while(n--){scanf("%d",&code);team[code]=i;}
        }        //!2.模拟
        queue<int> q,q2[maxn];       
         while(1){           
          int x;            
         string cmd;            
         cin>>cmd;            
         if(cmd[0] == 'S')break;           
          else if(cmd[0] == 'D'){
                x=q.front();                
                cout<<q2[x].front()<<endl;
                q2[x].pop();                
                if(q2[x].empty())q.pop();
            }            else if(cmd[0] == 'E'){                cin>>x;                int t=team[x];                if(q2[t].empty())q.push(t);
                q2[t].push(x);
            }
        }        cout<<endl;
    }    return 0;
}

keep going

作者:MarkKobs

原文链接:https://www.cnblogs.com/MarkKobs-blog/p/10459979.html


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP