本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2021-06-17 17:19:37
定义一个队列数组,以及一个队列,分别贮存小组里有多少元素和小组的个数。
输入完小组后,输入 $m$ 次字符串 $s$,判断字符串 $s$,如果是 "push",就让属于这个小组的队列插入,如果没有这个小组就让小组队列插入这个小组;否则,输出现在队列中第一个小组的元素,然后再让这个元素弹出,再判断如果这个小组没有元素了,就让这个小组弹出。
最后献上代码:
#include <cstdio>
#include <string>
#include <queue>
#include <iostream>
using namespace std;
int main() {
queue<int> s[301];
queue<int> x;
int m,n,a[100005];
cin>>n>>m;
for(int i=0;i<n;++i)
cin>>a[i];
int t;
cin>>t;
while(t--){
string s1;
cin>>s1;
if(s1=="push"){
int p;
cin>>p;
if(s[a[p]].empty())
x.push(a[p]);
s[a[p]].push(p);
}
else{
cout<<s[x.front()].front()<<endl;
s[x.front()].pop();
if(s[x.front()].empty())
x.pop();
}
}
return 0;
}

鲁ICP备2025150228号