https://vjudge.net/contest/240386#problem/F
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example. In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue. Your task is to write a program that simulates such a team queue.
Input
The input file will contain one or more test cases. Each test case begins with the number of teams t (1 ≤ t ≤ 1000). 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 queue
• STOP — end of test case The 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.
Output
For each tes tcase, 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.
Sample Input
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
Sample Output
Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001
这道题搞了我一整天,最后终于AC了,总结一下自己所犯的错误,努力注意不要让下次在发生,写到这儿,我就想哭了
不管了,不管多艰难都要坚持下来,加油!
首先自己的想法:将队员和队号对应,我采用的对应方式是数据,即a[j][i],j代表队号,然后将j按照所要求的操作以某种顺序存入queue<int> q[ m ],中,然后读队首即可;想法虽好,但操作不可实现,没办法将要求操作的j依次存入m中;
正确的想法是:先把队号和队员对应,(map<int,int>m[ i ]=t;)然后按照操作将队员放入q1[ m[ i ] ].push( i );同时将队号存入
q.push(m[ i ]),存入时还要判断q1[ m[ i ] ]这个队为不为空,为空时存入,不为空时不存,输出时按照q1[ q.front( ) ].front( )输出并移除;
还有一些小问题,就是cin读取时要清空缓存区,否则下一次读取时会自动读取缓存区里的内容,造成输入无效;
queue map等一次循环后都要清空,所以就定义在循环结构里,否则会造成未知的错误;大致如此了,以后一定要注意,同时一定要将想法弄清,否则会很艰难!!!
正确答案:AC代码
刚开始的错误代码(WA):
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
int t,n,e;
int ei[1010][1010];
queue<int> q[1010];
map<int,int> mp;
int main()
{
char s[100];
int k=1;
int c,cp;
while(~scanf("%d",&t))
{
if(t==0) break;
c=1;
for(int j=0;j<t;++j)
{
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%d",&ei[j][i]);
}
printf("Scenario #%d\n",k++);
while(1)
{
scanf("%s",s);
if(s[0]=='E')
{
scanf("%d",&e);
for(int j=0;j<t;++j)
for(int i=0;i<n;++i)
if(e==ei[j][i])
{
if(c==1) mp[j]=c,c++;
for(int m=1;m<c;++m)
{
if(mp[j]==m) {cp=0;break;}
else cp=1;
}
if(cp) mp[j]=c,c++;
q[mp[j]].push(e);
}
}
if(s[0]=='D')
{
int j=0;
while(1)
if( q[j].empty()) j++;
else { printf("%d\n",q[j].front());q[j].pop(); break;}
}
if(s[0]=='S') { printf("\n");break; }
}
}
return 0;
}