Team Queue
Team Queue |
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 itsteammates (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 file will contain one or more test cases. Each test case begins with the number of teams t (
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.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
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
刚开始还想用搜索的,后来发现没必要。
一次就Ac,哈哈,心宽体胖。
关键是 a,b数组,a数组是一个映射,从人对应到队伍编号。
b数组也是一个映射,从队伍编号对应到block。
queue 用rear 和total。
进来一个人,你先看它所在的队伍是不是已经存在,如果存在,那么直接加进去。
如果不存在,那么创新的。
#include <iostream> #include <cstdio> #include <fstream> #include <cstring> #include <stack> #include <string.h> using namespace std; int a[1000005],b[1005]; int which_team(int num){return a[num];} int team_in_which_block(int num){return b[num];} struct teamT{ int rear,total; int people[1005]; }; teamT block[1005]; int b_rear,b_total; int main() { //ifstream fin("ha.txt"); int team_nums,m,person,team_id,block_id,count_t=1; char command[10]; while(cin>>team_nums&&team_nums){ memset(a,-1,sizeof(a)); memset(b,-1,sizeof(b)); b_rear=0;b_total=0; for(int i=0;i<team_nums;i++){ cin>>m; for(int j=0;j<m;j++){ cin>>person; a[person]=i; } } cout<<"Scenario #"<<count_t++<<endl; while(cin>>command){ if(command[0]=='S')break; else if(command[0]=='E'){ cin>>person; team_id = which_team(person); block_id=team_in_which_block(team_id); if(block_id==-1){ //队伍还没出现 block[b_total].total=0;block[b_total].rear=0; block[b_total].people[block[b_total].total++]=person; b[team_id]=b_total; b_total++; } else{ //已经存在了 block[block_id].people[block[block_id].total++]=person; } } else{ //开始删除人物了 //block[b_rear] cout<<block[b_rear].people[block[b_rear].rear++]<<endl; if(block[b_rear].rear>=block[b_rear].total){ team_id=which_team(block[b_rear].people[0]); b[team_id]=-1; b_rear++; } } } cout<<endl; } return 0; }
Team Queue
Team Queue |
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 itsteammates (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 file will contain one or more test cases. Each test case begins with the number of teams t (
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.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
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