int T; intmain(){ queue<int> q,q1;//两个队列交替进行 int n; cin>>T; while(T--) { cin>>n; for (int i = 1; i <= n; i++) { q.push(i);//先把人(编号)入队 } int cnt=0;//计数器 while(q.size()>3) { cnt=0; while (!q.empty()) { int v=q.front(); q.pop(); cnt++; if (cnt%2==1) { q1.push(v);//保留非2的人 } } if (q1.size()<=3) {//一定要在这里判断一下,这个很重要,如果没有这步会出现漏项 while (!q1.empty()) { q.push(q1.front()); q1.pop(); } break; } cnt=0;//重置 while (!q1.empty()) {
int v=q1.front(); q1.pop(); cnt++; if (cnt%3==1||cnt%3==2) { q.push(v);//保留非3的人 } } } while (!q.empty()) { cout<<q.front()<<" "; q.pop();//出队 } cout<<endl; } return0; }
intmain(){ int cnt=0; while (cin>>T&&T) { cnt++; cout<<"Scenario #"<<cnt<<endl; memset(hax,0,sizeof(hax));//初始化哈希 for (int i=1;i<=T;i++) { int n; cin>>n; while (n--) { int x; cin>>x; hax[x]=i;//建立索引,就是以元素为索引,对应的队伍编号为元素,这样方便查找 } } queue<int> q[10001]; int s=0,e=0;//整个队列区间 string str; while (cin>>str) { if (str=="STOP") break; if (str=="ENQUEUE") { int x,i;//x是元素,i是行数 cin>>x; for (i=s;i<e;i++) { if (hax[q[i].front()]==hax[x]) break; //这里是通过访问每个队队首元素带回哈希表,再与元素所在队伍进行比较,判定是否在同一个队伍 } q[i].push(x);//进入对应的队伍 if (i==e) e++;//如果这个队伍还不存在需要拓展一下队伍 } else { int val=q[s].front(); q[s].pop(); if (q[s].empty()) s++;//如果已经空了就取消这个队伍 cout<<val<<endl;