zoukankan      html  css  js  c++  java
  • UVA 540 Team Queue(模拟+队列)

    题目代号:UVA 540

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=481

    题目描述:

    Team Queue

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2645 Accepted Submission(s): 910

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

    题目大意:给你有n个队伍。 对于每个ENQUEUE  x 命令。 如果x所在的队伍已经在队列中, 则x排在队列中它的队伍的尾巴, 否则排在队列的末尾。 可以理解为队列中的队列的味道。对于DEQUEUE命令输出总队列中第一个小组的第一个元素。对于STOP命令,代表停止命令的输入。

    解题思路:代码中的注释有详细描述。

    AC代码:

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <set>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    char str[10];
    int team[1000005];//最大编号不会超过999999所以用这个来表示编号所属的小组号
    
    int main()
    {
        int n,t,num,cases=1;
        while(scanf("%d",&t),t)
        {
            mem(team,0);
            queue<int>Q[1005];
            queue<int>que;//这样定义在while内就不需要通过循环清空队列,循环一次队列就初始化一次
            for(int i=1; i<=t; i++)
            {
                scanf("%d",&n);
                for(int j=1; j<=n; j++)
                {
                    scanf("%d",&num);
                    team[num]=i;//初始化该编号成员所属小组
                }
            }
            printf("Scenario #%d\n",cases++);
            do
            {
                scanf("%s",str);
                if(!strcmp(str,"ENQUEUE"))
                {
                    scanf("%d",&num);
                    if(Q[team[num]].empty())que.push(team[num]);//如果该编号成员所属小组的队列没有人,则在总队列尾部添加该小组
                    Q[team[num]].push(num);//小组所属的队列尾部添加该成员
                }
                else if(!strcmp(str,"DEQUEUE"))
                {
                    printf("%d\n",Q[que.front()].front());//输出总队列中首部的小组所对应的第一个成员
                    Q[que.front()].pop();//删除已经输出编号的成员
                    if(Q[que.front()].empty())//如果该小组人已经没人了,则在总队列中删除这个小组,之后的循环是对下一个小组进行操作
                        que.pop();
                }
            }
            while(strcmp(str,"STOP"));
            cout<<endl;
        }
        return 0;
    }

      

  • 相关阅读:
    转:IOCP在网络应用中常见错误分析
    Ext.Button的禁用
    Excel连接字符串
    从表单为实体对象赋值
    根据指定类型创建数组
    Ext.GridPanel数据显示不正确
    vue 记事本
    杂谈(一)
    推荐《程序设计的 Top 10 做与不做》和《关于编程,鲜为人知的真相》
    (转)黄鸣博客:警惕29岁现象
  • 原文地址:https://www.cnblogs.com/teble/p/7204394.html
Copyright © 2011-2022 走看看