NC50966. Team Queue
描述
输入描述
The input will contain one or more test cases. Each test case begins with the number of teams t . Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
ENQUEUE x - enter element x into the team queue
DEQUEUE - process the first element and remove it from the queueSTOP - end of test caseThe input will be terminated by a value of 0 for t.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
输出描述
For each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.
示例1
输入:
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
C++(g++ 7.5.0) 解法, 执行用时: 109ms, 内存消耗: 1532K, 提交时间: 2023-07-24 11:08:24
#include<bits/stdc++.h> using namespace std; queue<int>q[1001]; int a[1000001]; int main(){ int x,t,n,tmp=0;string s; while(scanf("%d",&t)&&t){ printf("Scenario #%d\n",++tmp); while(q[0].size())q[0].pop(); for(register int i=1;i<=t;++i){ scanf("%d",&n); while(q[i].size())q[i].pop(); while(n--)scanf("%d",&x),a[x]=i; } while(cin>>s&&s!="STOP"){ if(s=="ENQUEUE"){ scanf("%d",&x); if(q[a[x]].empty())q[0].push(a[x]); q[a[x]].push(x); } else{ if(q[0].size())printf("%d\n",q[q[0].front()].front()),q[q[0].front()].pop(); if(q[q[0].front()].empty())q[0].pop(); } } puts(""); } }
C++11(clang++ 3.9) 解法, 执行用时: 29ms, 内存消耗: 1504K, 提交时间: 2020-02-04 17:29:36
#include<bits/stdc++.h> using namespace std; int T,k,n,x,f[1000005],head[1005],tail[1005],q[1005][1005]; char s[10]; int main(){ while(scanf("%d",&T)&&T){ for(int i=1;i<=T;++i){ scanf("%d",&n);head[i]=tail[i]=1; while(n--){scanf("%d",&x);f[x]=i;} } printf("Scenario #%d\n",++k);head[0]=tail[0]=1; while(scanf("%s",s)&&s[0]!='S'){ if (s[0]=='E'){ scanf("%d",&x); if (!(tail[f[x]]-head[f[x]]))q[0][tail[0]++]=f[x]; q[f[x]][tail[f[x]]++]=x; } else{ x=q[0][head[0]];printf("%d\n",q[x][head[x]++]); if (!(tail[x]-head[x]))++head[0]; } } printf("\n"); } return 0; }