• UVA 540(队列)


    Description

    Download as PDF


      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
    



    Miguel A. Revilla
    1999-01-11

    队列应用

    题意:让你排一个长队,ENQUEUE -X 就是往队列里加一个元素,假设这个元素跟队列里的某个元素在上边给出的team里是在同样的team。则将这个元素插在这个元素的右边,否则放在总队列的最后 。

    用一个队列保存队伍编号。还有一些队列保存编号每一个队的队员。

    #include<stdio.h>
    #include<algorithm>
    #include<cctype>
    #include<queue>
    #include<string.h>
    using namespace std;
    int order[1000001],exist[1000001];
    queue<int>team[1001];
    queue<int>cnt;
    
    int main()
    {
        int t,cas=1;
        //freopen("in.txt","r",stdin);
        char s[15];
        while(~scanf("%d",&t)&&t)
        {
            int n=0,num;
            for(int i=0;i<t;i++)
            {
                scanf("%d",&n);
                for(int j=1;j<=n;j++)
                {
                    scanf("%d",&num);
                    order[num]=i;
                }
            }
            for(int i=0;i<=t;i++)
            {
                while(!team[i].empty())team[i].pop();
            }
            while(!cnt.empty())cnt.pop();
            memset(exist,0,sizeof(exist));
            printf("Scenario #%d
    ",cas++);
            while(~scanf("%s",s))
            {
                if(strcmp(s,"STOP")==0)break;
                else if(strcmp("DEQUEUE",s)==0)
                {
                    int p=cnt.front();
                    int ans=team[p].front();team[p].pop();
                    printf("%d
    ",ans);
                    if(team[p].empty())
                    {
                        cnt.pop();
                        exist[p]=0;
                    }
                }
                else
                {
                    scanf("%d",&num);
                    int &temp=order[num];
                    team[temp].push(num);
                    if(!exist[temp])
                    {
                        cnt.push(temp);
                        exist[temp]=1;
                    }
                }
            }
            printf("
    ");
        }
        return 0;
    }




  • 相关阅读:
    手机号 验证(正则)
    静态库相关
    添加友盟SDK,实现新浪分享和微信朋友圈分享的关键代码
    删除Mac 系统中多出得MobileBackups 文件夹
    浏览器判断检测手机中是否安装了ios/android客户端程序
    浏览器判断是否安装了ios/android客户端程序
    自定义TextField的删除按钮
    应用崩溃日志解析
    关于UITextView的一些问题
    关于UIColor这个类的一些不知道的事
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6709451.html
Copyright © 2020-2023  润新知