UVA 540 —— Team Queue

原題:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=481

題意:有n組人,給出每組人的編號;

有兩種操作 —— 1、ENQUEUE X 表示將編號爲X的人入隊,如果隊列當中已經有與X同組的成員,那麼就將X排在同組的最後一個成員的後面;如果隊列中沒有與X同組的成員,則將X排在整個隊列的末尾;2、DEQUEUE 表示輸出當前隊首的編號並出隊;

思路:開兩個隊列,一個用來儲存每組在隊的成員,另一個用來儲存在隊的組別(成員集合);


#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 1100;
queue<int>Q, q[maxn];
int id[10000000];
int n, cas = 0;
int main()
{
	while(~scanf("%d", &n))
	{
		if(n == 0)	break;
		printf("Scenario #%d\n", ++cas);
		while(!Q.empty())	Q.pop();
		for(int i = 1;i<=n;i++)
		{
			int m;
			while(!q[i].empty())	q[i].pop();
			scanf("%d", &m);
			for(int j = 1;j<=m;j++)
			{
				int x;
				scanf("%d", &x);
				id[x] = i;
			}
		}
		char cmd[10];
		scanf("%s", cmd);
		while(cmd[0] != 'S')
		{
			if(cmd[0] == 'E')
			{
				int k;
				scanf("%d", &k);
				int t = id[k];				
				if(q[t].empty())	
				Q.push(t);
				q[t].push(k);
			}
			else
			{					
				int top = Q.front();
				if(q[top].empty())	
				{
					Q.pop();
					top = Q.front();
				}				
				printf("%d\n", q[top].front());
				q[top].pop();
			}
			scanf("%s", cmd);
		}
		printf("\n");
	}
	return 0;
}


發佈了60 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章