hdu 1387 Team Queue

来源:互联网 发布:工作照软件 编辑:程序博客网 时间:2024/05/29 03:19

Team Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2741    Accepted Submission(s): 944


Problem Description
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 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.

 

Output
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. 

题意:每一次有人排队,如果前面有他的队友,那么就排在队友的最后面,如果没有队友则排在队伍最后面

分析:可以设一个总的队列,里面存team的编号,多个小队列存入队的值

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>using namespace std;int main(){    int n;    int k=1;    while(~scanf("%d",&n)&&n)    {        queue <int> q[1005],w;        int ci[1005];        memset(ci,0,sizeof(ci));        map<int ,int> m;        int team=1;        for(int i=1;i<=n;i++)        {            int num;            scanf("%d",&num);            for(int j=1;j<=num;j++)            {                int x;                scanf("%d",&x);                m[x]=team;            }            team++;        }        char s[10];        printf("Scenario #%d\n",k++);        while(cin>>s)        {            if(s[0]=='S') break;            if(s[0]=='E')            {                int x;                scanf("%d",&x);                if(!ci[m[x]])                    w.push(m[x]);                q[m[x]].push(x);                ci[m[x]]++;//存每个小队的人数            }            else            {                int t=w.front();                if(ci[t]!=0)                {                    printf("%d\n",q[t].front());                    ci[t]--;                    q[t].pop();                    if(ci[t]==0)                    {                        w.pop();//如果小队列没人了,那么新加入的就只能排在队伍末尾                    }                }            }        }        printf("\n");    }    return 0;}