zoukankan      html  css  js  c++  java
  • Team Queue

    Team Queue
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 2471   Accepted: 926

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

     1 #include"iostream"
     2 #include"map"
     3 #include"queue"
     4 #include"cstring"
     5 #include"string"
     6 #include"cstdio"
     7 using namespace std;
     8 //map<int,int> m;
     9 int m[1000006];
    10 queue<int> q;//队伍序号 
    11 queue<int> qq[1005];//入队 
    12 int flag[1005];
    13 int ncase=0;
    14 int t,n,ele;
    15 void inti();
    16 void input();
    17 void solve(); 
    18 int main()
    19 {
    20     while(scanf("%d",&t)==1&&t)
    21     {
    22         inti();
    23         input();
    24         solve();
    25     }
    26     return 0;
    27 } 
    28 void input()
    29 {
    30     for(int i=0;i<t;i++)
    31     {
    32         //cin>>n;
    33         scanf("%d",&n);
    34         for(int j=0;j<n;j++)
    35         {
    36             //cin>>ele;
    37             scanf("%d",&ele);
    38             m[ele]=i;
    39         }
    40     }
    41 }
    42 void inti()
    43 {
    44     for(int i=0;i<t;i++)
    45     {
    46         flag[i]=0;
    47         while(!qq[i].empty())
    48             qq[i].pop();
    49     }
    50     while(!q.empty())
    51        q.pop();
    52 }
    53 void solve()
    54 {
    55     string com;
    56     int a;
    57     //cout<<"Scenario #"<<++ncase<<endl;
    58     printf("Scenario #%d
    ",++ncase);
    59     while(cin>>com,com!="STOP")
    60     {
    61         if(com=="ENQUEUE")
    62         {
    63             //cin>>a;
    64             scanf("%d",&a);
    65             if(!flag[m[a]])
    66             {
    67                 flag[m[a]]=1;
    68                 q.push(m[a]);
    69             }
    70             qq[m[a]].push(a);
    71         }
    72         else if(com=="DEQUEUE")
    73         {
    74             int order=q.front();
    75             cout<<qq[order].front()<<endl;
    76             qq[order].pop();
    77             if(qq[order].empty())
    78             {
    79                 q.pop();
    80                 flag[order]=0;
    81             }
    82         }
    83     }
    84     cout<<endl;
    85 }
    View Code
  • 相关阅读:
    [译] 我最终是怎么玩转了 Vue 的作用域插槽
    通俗易懂的Git使用入门教程
    JS取出两个数组的不同或相同元素
    jQuery中四种事件监听的区别
    vuex里mapState,mapGetters使用详解
    php 获取时间今天明天昨天时间戳
    Linux crontab定时执行任务
    php返回json数据函数实例_php技巧_脚本之家
    mysql查看表结构命令
    Mysql命令大全
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3760934.html
Copyright © 2011-2022 走看看