开一个大队列, 里面放队伍的编号, 因为一个队伍里的所有人要么没入队, 要么在队列的一段连续位置, 可以用一个位置代表. 而每段连续位置又构成一个小队列, 所以再给每个队伍开一个小队列模拟即可.
#include<cstdio> #include<cstring> #include<queue> #define in inline #define re register using namespace std; in int read() { int s(0),b(0);char ch; do{ch=getchar();if(ch=='-')b=1;}while(ch<'0'||ch>'9'); while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar(); if(b) return -s; return s; } queue<int> q[1001]; int f[1000001]; signed main() { int cnt=0; while(1) { ++cnt; int t=read(); if(!t) break; for(re int i=1;i<=t;++i) { int n=read(); for(re int j=1;j<=n;++j) f[read()]=i; } char s[10]; printf("Scenario #%d\n",cnt); while(1) { scanf("%s",s); if(s[0]=='E'){ int x=read(); if(q[f[x]].empty()) q[0].push(f[x]); q[f[x]].push(x); } if(s[0]=='D'){ int x=q[0].front(); printf("%d\n",q[x].front()); q[x].pop(); if(q[x].empty()) q[0].pop(); } if(s[0]=='S') break; } for(re int i=0;i<=t;++i) while(!q[i].empty()) q[i].pop(); printf("\n"); } return 0; }
最新评论