UVA 540 (队列与map容器)

题目大意:n条队伍,然后开始组成一条长队,如果长队中已经有自己人,直接插队插到自己人最后面,如果没有乖乖排最后。


题目思路:使用map容器记录每个人对应的队伍,然后插入的时候判断队里有没有自己人,如果没有的话就把对应的队伍号放到长队q的最后面,输出的时候先看长队q.front()对应的那个队伍还有没有人,没人的话就让下一个队伍输出


以下是代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<queue>
using namespace std;
int main(){
	int n,a,b,num=0;
	string cmd;
	while(~scanf("%d",&n)&&n){
		printf("Scenario #%d\n",++num);
		map<int,int>team;
		queue<int>q,q2[1005];
		for(int i=0;i<n;i++){
			scanf("%d",&a);
			for(int j=0;j<a;j++){
				scanf("%d",&b);
				team[b]=i;
			}
		}
		while(cin>>cmd){
			if(cmd[0]=='S'){
				break;
			}
			else if(cmd[0]=='E'){
				int temp,t;
				scanf("%d",&temp);
				t=team[temp];		
				if(q2[t].empty()){
					q.push(t);
				}
				q2[t].push(temp);
			}
			else if(cmd[0]=='D'){
				int t=q.front();
				while(q2[t].empty()){
					q.pop();
					t=q.front();
				}
				printf("%d\n",q2[t].front());
				q2[t].pop();
			}
		}
		printf("\n");
	}
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值