描述
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.
输入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.
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.输出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.样例输入
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
样例输出
Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001
题意:给定不重复的数字表示第i组第j人,有两种操作:
- 插入队尾
- 如果有同组的,那么插入到同组的人后面
- 没有则老老实实排队
- 队头出队
题解:我们不关心每个人在队里的编号,我们只要知道这个编号是哪个队的,所以可以用标记数组或者map进行标记。
然后开n+1个队列加一个输出队列
- 操作1:
- 判断该编号所属队中是否存在元素,不存在则将该编号所在队号加入q0中。
- 将编号加入所属队中。
- 操作2:
- 将q0中的队首赋给变量sign,将第sign队队首取出并加入输出队列中。
- 判断第sign队是否为空,是则取出q0的队首。
//#include<bits/stdc++.h> #include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; const int INF = 0x3f3f3f3f; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } using namespace std; const int maxn=1e5+10; map<int,int>ma; template <class T> inline bool scan_d(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; //EOF while(c!='?'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='?')?-1:1; ret=(c=='?')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } inline void outt(int x) { if(x>9) outt(x/10); putchar(x%10+'0'); } queue<int>q[1010]; queue<int>out; int main() { int n,m,tmp,cnt=0;; while(~scanf("%d", &n)&&n) { rep(k,1,n) { scan_d(m); rep(l,1,m) { scan_d(tmp); ma[tmp]=k; } } rep(i,0,n) { while(!q[i].empty())q[i].pop(); } while(!out.empty())out.pop(); char s[10]; while(scanf("%s", s)&&s[0]!='S') { if(s[0]=='E') { scan_d(tmp); int sign=ma[tmp]; if(q[sign].size()==0) { q[0].push(sign); } q[sign].push(tmp); } else { int sign=q[0].front(); out.push(q[sign].front()); q[sign].pop(); if(q[sign].size()==0) { q[0].pop(); } } } cout<<"Scenario #"<<++cnt<<endl; while(!out.empty()) { outt(out.front()); out.pop(); printf("\n"); } cout<<endl; } return 0; }