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

Description

Download as PDF
 
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 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 le t le 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


根据题目意思,这个team队列,只需要考虑当前team里面是否有人在排队,
故可以每个team派一个代表去排队,然后team内部同时进行排队即可。而这个代表实用team脚标即可。而只有当team内部人全部出队后,team才出队。
然而需要快速查找每个人属于哪个team,需要使用一种映射方式,此处可以采用了map,也可以直接数组,因为数据不是很大。


代码1:采用stl队列
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long

using namespace std;

int t;
int Hash[1000005];
bool vis[1005];
queue<int> q[1005];

void input()
{
    memset(Hash, -1, sizeof(Hash));
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < 1005; ++i)
        while (!q[i].empty())
            q[i].pop();
    int num, id = 0, tmp;
    for (int i = 0; i < t; ++i)
    {
        scanf("%d", &num);
        for (int i = 0; i < num; ++i)
        {
            scanf("%d", &tmp);
            Hash[tmp] = id;
        }
        id++;
    }
}

void push(queue<int> &teamQ, int k)
{
    if (!vis[Hash[k]])
    {
        vis[Hash[k]] = true;
        teamQ.push(Hash[k]);
    }
    q[Hash[k]].push(k);
}

void pop(queue<int> &teamQ)
{
    int k = teamQ.front(), out;
    out = q[k].front();
    q[k].pop();
    if (q[k].empty())
    {
        teamQ.pop();
        vis[k] = false;
    }
    printf("%d
", out);
}

void work()
{
    queue<int> teamQ;
    char str[15];
    int k;
    for (;;)
    {
        scanf("%s", str);
        if (str[0] == 'S')
            return;
        if (str[0] == 'E')
        {
            scanf("%d", &k);
            push(teamQ, k);
        }
        else
            pop(teamQ);
    }
}

int main()
{
    //freopen("test.in", "r", stdin);
    int times = 1;
    while (scanf("%d", &t) != EOF && t)
    {
        printf("Scenario #%d
", times++);
        input();
        work();
        printf("
");
    }
    return 0;
}
View Code


此处team内部排队使用了链式结构的Queue。
代码2:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10

using namespace std;

struct isQueue
{
    int val;
    isQueue *next;
};

struct Queue
{
    isQueue *top;
    isQueue *rear;
    void Init()
    {
        top = (isQueue *)malloc(sizeof(isQueue));
        rear = (isQueue *)malloc(sizeof(isQueue));
        top->next = rear;
        rear->val = -1;
    }
    void Pop()
    {
        top = top->next;
    }
    int Front()
    {
        return top->next->val;
    }
    void Push(int k)
    {
        isQueue *v = (isQueue *)malloc(sizeof(isQueue));
        rear->val = k;
        rear->next = v;
        rear = rear->next;
        rear->val = -1;
    }
    bool Empty()
    {
        if (top->next->val == -1)
            return 1;
        else
            return 0;
    }
};

struct node
{
    int isin;
    Queue turn;
};

node p[1005];
map <int, int> Hash;
int t;

void Init()
{
    Hash.clear();
    int k, v;
    for (int i = 0; i < t; ++i)
    {
        p[i].isin = 0;
        p[i].turn.Init();
        scanf("%d", &k);
        for (int j = 0; j < k; ++j)
        {
            scanf("%d", &v);
            Hash[v] = i;
        }
    }
}

void qt()
{
    char ch[20];
    int num;
    int x;
    queue <int> q;
    for (;;)
    {
        scanf("%s", ch);
        if (ch[0] == 'S')
        {
            printf("
");
            return;
        }
        if (ch[0] == 'E')
        {
            scanf("%d", &num);
            x = Hash[num];
            if (p[x].isin == 0)
            {
                q.push(x);
                p[x].turn.Push(num);
                p[x].isin++;
            }
            else
            {
                p[x].turn.Push(num);
                p[x].isin++;
            }
        }
        else if (ch[0] == 'D')
        {
            x = q.front();
            num = p[x].turn.Front();
            if (p[x].isin == 1)
                q.pop();
            p[x].turn.Pop();
            p[x].isin--;
            printf("%d
", num);
        }
    }
}

int main()
{
    //freopen ("test.txt", "r", stdin);
    int times = 1;
    while (scanf("%d", &t) != EOF && t != 0)
    {
        printf("Scenario #%d
", times);
        Init();
        qt();
        times++;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/andyqsmart/p/4162399.html