Description
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 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 file will contain one or more test cases. Each test case begins with the number of teams t ( ). 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.
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
Miguel A. Revilla
1999-01-11
题目大意:
先给几组queue,然后输入ENQUEUE和DEQUEUE,输入ENQUEUE时,把数字入新队列,若此数字所在的组在新队列里面未出现过,则放在新队列的最后面,若出现过,则放在新队列里此组的最后面,当为DEQUEUE时,把新队列的第一个元素取出来。
解题思路:
通过在新队列里面放子队列实现。
代码一:
#include<iostream>
#include<queue>
#include<cstdio>
#include<map>
#include<memory.h>//memset的头文件.
using namespace std;
int team[1000000];
int t;
char s[40];
bool visited[1100];
queue<int> q[1100],realqueue;
void solve(){
while(!realqueue.empty()) realqueue.pop();
int n,member;
for(int i=0;i<t;i++){
scanf("%d",&n);
while(!q[i].empty()) q[i].pop();
for(int j=0;j<n;j++){
scanf("%d",&member);
team[member]=i;
}
}
memset(visited,0,sizeof(visited));
while(scanf("%s",s)!=EOF){
if(*s!='S'){
if(*s=='E'){
scanf("%d",&member);
q[team[member]].push(member);//queue里面没有push_back,但是有push.
if(visited[team[member]]==false){
realqueue.push(team[member]);
visited[team[member]]=true;
}
}
else{
printf("%d\n",q[realqueue.front()].front());
q[realqueue.front()].pop();
if(q[realqueue.front()].empty()){
visited[realqueue.front()]=false;
realqueue.pop();
}
}
}
else
break;
}
}
int main(){
int casen=0;
while(scanf("%d",&t)!=EOF&&t){
printf("Scenario #%d\n",++casen);
solve();
printf("\n");
}
return 0;
}
#include <iostream>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
int pri,num;
node(int pri0=0,int num0=0){
pri=pri0;num=num0;
}
friend bool operator <(node a,node b){
if(a.pri!=b.pri) return a.pri<b.pri;
else return a.num<b.num;
}
};
map <node,int> mp;
int cnt[1100],pri[1100],color[1000000];
int m,p,num;
void initial(){
mp.clear();
for(int i=0;i<m;i++){
cnt[i]=pri[i]=0;
}
p=num=0;
}
void input(){
for(int i=0;i<m;i++){
int k,x;
scanf("%d",&k);
while(k-- >0){
scanf("%d",&x);
color[x]=i;
}
}
}
void solve(){
char st[40];
while(scanf("%s",st)!=EOF && st[0]!='S' ){
int x;
if(st[0]=='E'){
scanf("%d",&x);
if(cnt[color[x]]>0){
mp[node(pri[color[x]],++num)]=x;
cnt[color[x]]++;
}else{
pri[color[x]]=++p;
cnt[color[x]]++;
mp[node(pri[color[x]],++num)]=x;
}
}else{
if(mp.size()>0){
int ans= mp.begin()->second;
cnt[color[ans]]--;
mp.erase(mp.begin());
printf("%d\n",ans);
}
}
}
}
int main(){
int casen=0;
while(scanf("%d",&m)!=EOF && m){
initial();
input();
printf("Scenario #%d\n",++casen);
solve();
printf("\n");
}
return 0;
}