1.用两个队列来维护成员,一个队列维护队伍编号,一个队列数组维护每个队伍的成员
ps: 紫书p118
#include <bits/stdc++.h> using namespace std; int t, k, x, y; queue<int> allq; queue<int> q[12]; vector<int> v; string s1="ENQUEUE", s2="DEQUEUE", s3="STOP", s; int main() { while(cin>>t&&t!=0){ while(!allq.empty()) allq.pop(); for(int i=0;i<12;++i) while(!q[i].empty()) q[i].pop(); v.clear(); while(cin>>s&&s!=s3){ if(s==s1){ cin>>x>>y; if(q[x].empty()) allq.push(x);//如果x队列没人,就在allq中插入x q[x].push(y);//把y压入x队列 } else if(s==s2){ int temp=allq.front(); v.push_back(q[temp].front()); q[temp].pop(); if(q[temp].empty()) allq.pop(); } } printf("Scenario #%d\n",++k); for(int i=0;i<v.size();++i) cout<<v[i]<<endl; puts(""); } return 0; } /* 2 ENQUEUE 1 101 ENQUEUE 2 201 ENQUEUE 1 102 ENQUEUE 2 202 ENQUEUE 1 103 ENQUEUE 2 203 DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 2 ENQUEUE 1 101 ENQUEUE 2 201 ENQUEUE 1 102 ENQUEUE 1 103 ENQUEUE 1 104 ENQUEUE 1 105 DEQUEUE DEQUEUE ENQUEUE 2 202 ENQUEUE 2 203 DEQUEUE DEQUEUE ENQUEUE 5 501 ENQUEUE 4 401 DEQUEUE DEQUEUE STOP 0 */