首页 > 其他 > 详细

(hdu step 8.1.8)Team Queue(一个人排队时,如果前面有他认识的人,那么他可以直接排到他认识的人的后面,否则排在队伍的最后面。求这种情况下的出队情况)

时间:2015-03-24 10:59:08      阅读:503      评论:0      收藏:0      [点我收藏+]

题目:

Team Queue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 95 Accepted Submission(s): 48
 
Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example. 
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue. 

Your task is to write a program that simulates such a team queue. 

 
Input
The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements. 

Finally, a list of commands follows. There are three different kinds of commands: 

ENQUEUE x - enter element x into the team queue 
DEQUEUE - process the first element and remove it from the queue 
STOP - end of test case 
The input will be terminated by a value of 0 for t. 

 
Output
For each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one. 
 
Sample Input
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
 
Sample Output
Scenario #1
101
102
103
201
202
203

Scenario #2
259001
259002
259003
259004
259005
260001
 
 
Source
University of Ulm Local Contest 1998
 
Recommend
Eddy
 


题目大意:

              有很多人,每个人都属于一个小队。现在所有人要排一列队(跟那个小队没关系),根据enqueue或者dequeue指令行事。不过有一点 在enqueue的时候,如果队伍前面有熟人(同一个小队的人,就可以插到那个人的后面去)


相当于去食堂排一列队伍打饭,如果前面有同班同学就可以插队到后面,没有的话就从最后面排



题目分析:

               用队列来做。



代码如下:

/*
 * h.cpp
 *
 *  Created on: 2015年3月24日
 *      Author: Administrator
 */


#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <cstring>



using namespace std;

const int maxn = 1001;

bool visited[maxn];

int main(){

	int cnt = 1;

	int n;
	while(scanf("%d",&n)!=EOF,n){
		memset(visited,false,sizeof(visited));

		queue<int> q[maxn];//每个小队.存储的是每个个体的编号
		queue<int> que;//总队列。存储的是每个小队的编号
		map<int,int> team;//team[t] = i 。表示的是编号为t的人属于第i个小队


		int i;
		for(i = 0 ; i < n ; ++i){
			int m;
			scanf("%d",&m);

			while(m--){
				int teamnums;
				scanf("%d",&teamnums);

				team[teamnums] = i;//为每个编号的人初始化小队...
			}
		}


		printf("Scenario #%d\n",cnt++);

		string str;
		while(cin >> str){//不断地输入指令
			if(str == "STOP"){//如果当前指令是STOP
				break;//那么跳出循环
			}else if(str == "ENQUEUE"){//如果当前指令是ENQUEUE
				int t;
				scanf("%d",&t);

				q[team[t]].push(t);//将t插入到它所对应的小队中
				if(visited[team[t]] == false){//如果该小队还没有被访问过(还没有在总队列中)
					que.push(team[t]);//将该小队的序号插入到总队列中
					visited[team[t]] = true;//将该小队标记为已经访问过
				}
			}else if(str == "DEQUEUE"){//如果当前指令是DEQUEUE
				printf("%d\n",q[que.front()].front());//那么打印排在总队列排在队首的小队中排在队首的元素
				q[que.front()].pop();//将该小队中对手的元素出队

				if(q[que.front()].empty() == true){//如果该小队已经为空
					visited[que.front()] = false;//那么将该小队重新标记为为访问过
					que.pop();//将该小队的编号从总队列中出队..
				}
			}
		}

		printf("\n");
	}

	return 0;
}






(hdu step 8.1.8)Team Queue(一个人排队时,如果前面有他认识的人,那么他可以直接排到他认识的人的后面,否则排在队伍的最后面。求这种情况下的出队情况)

原文:http://blog.csdn.net/hjd_love_zzt/article/details/44588531

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!