首页 > 代码库 > Team Queue
Team Queue
Team Queue |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 129 Accepted Submission(s): 63 |
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 23 101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003DEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP0 |
Sample Output Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001 |
Source University of Ulm Local Contest 1998 |
Recommend Eddy |
/*题意:模拟队列,初始的情况有两个队列,现在将两个队列中去元素进行添加到一个新的队列中, 两种操作ENQUEUE a:添加元素a进入队列,如果队列中有它的“队友”就是原来和他在一个队的,就可以插到她的后边 DEQUEUE :队列的元素头的元素出队列初步思路:用queue进行模拟,建n个队列嘛,实际上如果队列中只要有一个某一个原始队列中的元素,那么从这个数开始 ,后边几个全是这个原始队列中的数,至于查询的时候可以用map进行查找*/#include<bits/stdc++.h>using namespace std;const int maxn = 1001;bool visited[maxn];void init(){ memset(visited,false,sizeof(visited));}int main(){ //freopen("in.txt","r",stdin); int cnt = 1; int n; while(scanf("%d",&n)!=EOF,n){ init(); queue<int> q[maxn];//每个小队.存储的是每个个体的编号 queue<int> que;//总队列。存储的是每个小队的编号 map<int,int> team;//team[t] = i 。表示的是编号为t的人属于第i个小队 for(int 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"){ 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;}
Team Queue
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。