题目:
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.
|
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.
|
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或者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;
}