UVA Team Queue

版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.csdn.net/u013840081/article/details/26180081

题目例如以下:

Team Queue 

Queues and Priority Queues are data structures which are known to most computer scientists. TheTeam Queue, however, is not so well known, though it occurs often in everyday life. At lunch timethe 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 thequeue from head to tail to check if some of its teammates (elements of the same team) are alreadyin the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail andbecomes the new last element (bad luck). Dequeuing is done like in normal queues: elements areprocessed 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 elementsbelonging 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 theimplementation of the team queue should be efficient: both enqueing and dequeuing of an elementshould 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 ablank 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

模拟题。这道题题意是对每一个元素。找到它在哪个队,假设已经有队友在排队,他就能够直接排在队友后面,否则排在队伍后面,输出每次出队时的元素。关键是要求入队和出队都是常数时间O(1)(由于command数量非常大)。所以通过遍历找到行号再插入是不行的(复杂度O(N) ),亲測TLE....后来发现用map就能够轻松实现常数时间内找到队伍号(由于能够使队伍号和元素相应起来),而且用二维数组存储结果队伍,第一个维是队伍号,第二个维是元素,这样实现常数时间插入。

肯定对于每支队伍还有队首。队尾指针来实现对 元素的控制。由于出队时是依照顺序一支队伍出队完毕后再出队下一支队伍。所以设置了一个order数组来记录入队的队伍的顺序,用vis来防止一支队伍进入order多次,用变量po来记录当前出队的队伍。easy忽略的细节是假设一支队伍已经出队完了。那它的vis应该变为0。由于接下来可能再入队这个队伍的元素,假设vis不清0的话,这支队伍没法进入order数组,这个细节非常隐蔽,因此WA了一次。。

最后还是成功AC了,代码并不长。

AC的代码例如以下:

#include 
#include
using namespace std;
int ans[1010][1010];
int main()
{
    int t,N=0;

    while((cin>>t)&&t!=0)
    {
        N++;
        cout<<"Scenario #"< TEAM,order;

        int m,team[1010],row,rear[1010]= {0},fron[1010]= {0},k=0,po=0,vis[1010]= {0};
        for(int i=0; i<=t-1; i++)
        {
            cin>>m;
            for(int j=0; j<=m-1; j++)
            {
                cin>>team[j];
                TEAM.insert(make_pair(team[j],i));
            }
        }
        string s;
        while(cin>>s)
        {
            if(s=="ENQUEUE")
            {
                cin>>m;
                row=TEAM[m];
                ans[row][rear[row]++]=m;
                if(vis[row]==0)
                {
                    order[k++]=row;
                    vis[row]=1;
                }
            }
            else if(s=="DEQUEUE")
            {
                cout<

原文地址:https://www.cnblogs.com/ldxsuanfa/p/9977400.html