队列
作者:互联网
132. 小组队列
输入样例:
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
arr[0]的队列记录每个组来的先后次序,之后的队列记录相应的组的成员
再按照arr[0]队列中的次序,依次使之后的队列出队
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
const int N = 1e7;
int id[N];//组号
int main(void)
{
int t, n, x, y, idx = 1;
string s;
while (cin >> t && t != 0){
queue<int> arr[1005];
for (int i = 1; i <= t; i++){
cin >> n;
for (int j = 1; j <= n; j++){
cin >> x;
id[x] = i;//记录每个成员的组号
}
}
cout << "Scenario #" << idx++ << endl;
while (cin >> s && s != "STOP"){
if (s == "ENQUEUE"){
cin >> x;
if (arr[id[x]].size() == 0){//如果是新的组
arr[0].push(id[x]);
}
arr[id[x]].push(x);
}
else if (s == "DEQUEUE"){
x = arr[0].front();
if (arr[x].size() == 0){
arr[0].pop();
x = arr[0].front();
}
y = arr[x].front();
arr[x].pop();
cout << y << endl;
}
}
cout << endl;
}
return 0;
}
默_silence
发布了170 篇原创文章 · 获赞 20 · 访问量 2万+
私信
关注
标签:arr,队列,DEQUEUE,ENQUEUE,int,id 来源: https://blog.csdn.net/weixin_43772166/article/details/104089451