Team Queue POJ - 2259 (队列)

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.

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

题意:n个小队,每个小队若干人。入队时,若队伍中已经有了该小队的人就直接排在其后面,没有的话就排在队伍后面。有入队和出队两种操作。思路:用队列进行模拟,map记录每个人属于哪支队伍,然后que【0】,记录队伍的顺序,que【i】记录该队伍的人。   1 <= i <= 1000

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

int t;
queue<int>que[1001];
map<int,int>mp;
int main()
{
    int cas = 0;
    while(~scanf("%d",&t) && t)
    {
        mp.clear();
        for(int i=0;i<=1000;i++)
        {
            while(!que[i].empty())que[i].pop();
        }
        int cnt = 0;
        while(t--)
        {
            int k,tmp;
            scanf("%d",&k);
            ++cnt;
            for(int i=1;i<=k;i++)
            {
                scanf("%d",&tmp);
                mp[tmp] = cnt;
            }
        }
        char s[20];
        printf("Scenario #%d\n",++cas);
        while(~scanf("%s",s) && s[0] != ‘S‘)
        {
            if(s[0] == ‘E‘)
            {
                int tmp;
                scanf("%d",&tmp);
                if(que[mp[tmp]].empty())que[0].push(mp[tmp]);
                que[mp[tmp]].push(tmp);
            }
            else
            {
                if(!que[0].empty())
                {
                    int tmp = que[0].front();
                    printf("%d\n",que[tmp].front());
                    que[tmp].pop();
                    if(que[tmp].empty())que[0].pop();
                }
            }
        }
        puts("");
    }
}

原文地址:https://www.cnblogs.com/iwannabe/p/10632488.html

时间: 2024-10-12 11:12:39

Team Queue POJ - 2259 (队列)的相关文章

POJ 2259 队列

链接: http://poj.org/problem?id=2259 题意: 排队吃饭,有n个team,每当一个人过来的时候,只要前面有他认识的人,就会直接插到那个team的后面,否则从后面排队 然后就是询问你每次出队的是谁 题解: 用两个队列就可以了,主队列里面是team编号,然后每个team再开一个队列就可以了 代码: 31 int n; 32 queue<int> Q[1010]; 33 queue<int> que; 34 int Hash[MAXN]; 35 36 int

UVa 540 Team Queue(团队队列)

题意  模拟团队队列的入队和出队 STL应用  用一个队列维护团队编号  再用一个队列数组维护个体 #include <cstdio> #include <cstring> #include <queue> #include <map> using namespace std; const int N = 1000005; int team[N]; int main() { int cas = 0, n, t, a; char cmd[20]; while(

queue POJ 2259 Team Queue

题目传送门 题意:先给出一些小组成员,然后开始排队.若前面的人中有相同小组的人的话,直接插队排在同小组的最后一个,否则只能排在最后面.现在有排队和出队的操作. 分析:这题关键是将队列按照组数分组,用另外一个队列保存组的序号,当该组里没有人了才换下一组.很好的一道题. 收获:队列的灵活运用 代码: /************************************************ * Author :Running_Time * Created Time :2015/9/9 星期三

POJ 2259 Team Queue 数据结构 队列

Team Queue Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3282   Accepted: 1188 Description Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, thoug

UVA - 540 Team Queue(STL,队列 )

Team Queue Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known,

ACM学习历程——UVA540 Team Queue(队列,map:Hash)

Description Team Queue   Team Queue  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

Team Queue

Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 129 Accepted Submission(s): 63   Problem Description Queues and Priority Queues are data structures which are known to most computer scie

Winter-2-STL-G Team Queue 解题报告及测试数据

Time Limit:3000MS     Memory Limit:0KB 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 q

HDU 1387 Team Queue

Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1889    Accepted Submission(s): 639 Problem Description Queues and Priority Queues are data structures which are known to most computer