HDU 1387 Team Queue

11 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=1387

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.

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

题目大意: 有一些人是互相认识的(同一队伍的),现在让你把一个人入队, 如果这个人认识的人已经在队列中(同一队伍的已经有人在队列中), 那么就把这个人插到该队伍的末端; 否则把这个人插到队列的末端。

思路: 首先我们用map<int,int> m建立队伍到队伍编号的映射, 比如样例1中我们令m[101]=m[102]=m[103]=1,令m[201]=m[202]=m[203]=2,方便我们以后查询。然后我们创建一个队列数组q来存储每个队伍的信息,比如q[i]存储队伍编号为i的队伍的信息;再创建一个队列que来存储总队列的信息,其实际存储的是队伍的编号,比如2号队伍在1号队伍后面,那么que中的元素就是1,2;方便我们进行出队操作。vis数组用来标记队伍i是否已经有元素在队列中。注意这道题的一个坑点,比如队伍i的成员已经全部出队了,再有队伍i的成员进队时要放到队列最后面。也就是说队列que是动态变化的。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
using namespace std;

char s[20]; //处理语句
int vis[1005];  //标记

int main()
{
    int n;
    int times=0;
    while(~scanf("%d",&n)&&n)
    {
        memset(vis,0,sizeof(vis));
        queue<int> q[1005];   //记录队伍
        queue<int> que;     //记录总队列顺序
        map<int,int> m;
        for(int i=1;i<=n;i++)
        {
            int temp;
            scanf("%d",&temp);
            for(int j=1;j<=temp;j++)
            {
                int t;
                scanf("%d",&t);
                m[t]=i; //标记该队伍的序号
            }
        }
        int temp;
        printf("Scenario #%d\n",++times);
        while(~scanf("%s",s)&&s[0]!='S')
        {
            if(s[0]=='E')   //入队
            {
                scanf("%d",&temp);
                int i=m[temp];
                if(vis[i])  //有伙伴在队列中
                    q[i].push(temp);
                else    //没有伙伴在队列中
                {
                    que.push(i);    //放到队尾
                    q[i].push(temp);
                    vis[i]=1;   //记录位置
                }
            }
            else    //出队
            {
                printf("%d\n",q[que.front()].front());
                q[que.front()].pop();
                if(q[que.front()].empty())//该队伍的最后一个人出队了
                {
                    vis[que.front()]=0;
                    que.pop();  //该队伍从总队列中出队
                }
            }
        }
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值