zoukankan      html  css  js  c++  java
  • Uva540(队列)

    Team Queue UVA - 540

    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 ≤ 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 Foreachtestcase, firstprintalinesaying‘Scenario #k’, where k isthenumberofthetestcase. 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<cstdio>
     3 #include<cstring>
     4 #include<map>
     5 #include<queue>
     6 const int maxn=1005;
     7 using namespace std;
     8 int n,m;
     9 int main()
    10 {
    11     int k=1;
    12     while(scanf("%d",&n)==1,n)
    13     {
    14         printf("Scenario #%d
    ",k++);
    15         map<int,int> mp;
    16         for(int i=0;i<n;i++)
    17         {
    18             scanf("%d",&m);
    19             while(m--){
    20                 int tot;
    21                 scanf("%d",&tot);
    22                 mp[tot]=i;//map里面键是唯一的mp[key]=value;
    23             }
    24         }
    25         queue<int>q,q2[maxn];
    26         while(1)
    27         {
    28             char c[10];
    29             scanf("%s",c);
    30             if(c[0]=='S')break;
    31             else if(c[0]=='D')
    32             {
    33                 int t=q.front();
    34                 printf("%d
    ",q2[t].front());
    35                 q2[t].pop();
    36                 if(q2[t].empty())q.pop();
    37             }
    38             else if(c[0]=='E')
    39             {
    40                 int x;
    41                 scanf("%d",&x);
    42                 int t=mp[x];//得到组数
    43                 if(q2[t].empty())q.push(t);
    44                 q2[t].push(x);
    45             }
    46         }printf("
    ");
    47     }
    48     return 0;
    49 }


    通过mp的key保存队员号,value保存组号,键值对中键的值是唯一的;

    通过q保存进入队列的组号;

    通过q2[]保存进入队列的各组的顺序。

  • 相关阅读:
    create-react-app搭建的项目中添加bootstrap
    用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
    为Docker配置阿里加速器,系统为Debian8
    基于Spring Boot,使用JPA动态调用Sql查询数据
    基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合
    基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD
    ES6,Array.includes()函数的用法
    【编程风格】c++命名约定
    【转】吴恩达的视频课程做成了文字版 ~~~
    【专业学习】常用的技术网站
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/11073305.html
Copyright © 2011-2022 走看看