UVA540 Team Queue 队列入门经典

题意翻译

有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会被排到长队的队尾。 输入每个团队中所有队员的编号,要求支持如下3中指令: ENQUEUE x:编号为x的人进入长队 DEQUEUE:长队的队首出队 STOP:停止模拟 对于每个DEQUEUE指令,输出出队的人的编号。

题目描述

PDF

输入输出格式

输入格式:

 

 

输出格式:

 

 

输入输出样例

输入样例#1: 复制

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

队列入门经典

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>            
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define N 1100 
typedef long long ll;
int main()
{
	int T,caseP = 1;
	 
	while(scanf("%d",&T)==1&&T)
	{  
	  map<int,int>team; //team[x]=i表示x队员属于i
	  for(int i=0;i<T;i++)
	  {
	  	int n;
	  	scanf("%d",&n);
	  	for(int j=1;j<=n;j++)
		{
			int x;
			scanf("%d",&x);
			team[x]=i;
		}
	  }
	  printf("Scenario #%d\n",caseP++);
	  queue<int>q,q2[N];
	  //q表示团队队列,q2表示各队的队列
	  while(1)
	  {
	  	string op;
	  	cin>>op;
	  	if(op[0]=='S')
	  		break;
		else if(op[0]=='D')
		{
		  int t=q.front();
		  printf("%d\n",q2[t].front());
		  q2[t].pop();
		  
		  if(q2[t].empty())
		  {
			q.pop(); //及时删除
		  }
		}
		else if(op[0]=='E')
		{
		 int x;
		 scanf("%d",&x);
		 int t=team[x];
		 if(q2[t].empty())
		 {
		 	q.push(t);
		 }
		 q2[t].push(x);	
		}
	  }
	  cout<<endl;	
	}

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值