zoukankan      html  css  js  c++  java
  • 8.1.8 Team Queue

    Team Queue

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

    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

    很恶心的一道题,要hash

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 #include <cstdlib>
     7 #include <queue>
     8 using namespace std;
     9 
    10 const int maxn=1000210;
    11 struct qq
    12 {
    13     int x,t,num;
    14     friend bool operator < (qq a,qq b)
    15     {
    16         if (a.num==b.num)
    17             return a.t>b.t;
    18         return a.num>b.num;
    19     }
    20 }ya; //num 表示在队列里它是属于第几个梯队的,t表示加入的时间,x表示这个数
    21 priority_queue<qq> q;
    22 int cnt,n,x,step;
    23 int team[maxn],num[maxn],tot[maxn];
    24 char s[20];
    25 
    26 void close()
    27 {
    28 exit(0);
    29 }
    30 
    31 
    32 void init()
    33 {
    34     int cas=0;
    35     while (scanf("%d",&n)!=EOF)
    36     {
    37         if (cas!=0)
    38             printf("
    ");
    39         if (n==0) break;
    40         while (!q.empty()) q.pop();
    41         cas++;
    42         printf("Scenario #%d
    ",cas);
    43         memset(num,-1,sizeof(num));
    44         for (int i=1;i<=n;i++)
    45         {
    46             scanf("%d",&cnt);
    47             for (int j=1;j<=cnt;j++)
    48             {
    49                 scanf("%d",&x);
    50                 team[x]=i;
    51             }
    52         }
    53         cnt=0;
    54         step=0;
    55         memset(tot,0,sizeof(tot));
    56         while (scanf("%s",s)!=EOF)
    57         {
    58             step++;
    59             if (s[0]=='S') break;
    60             if (s[0]=='E')
    61             {
    62                 scanf("%d",&x);
    63                 if (num[team[x]]!=-1 && tot[team[x]]!=0) //already in it
    64                 {
    65                     ya.num=num[team[x]];
    66                     tot[team[x]]++;
    67                 }
    68                 else
    69                 {
    70                     cnt++;
    71                     ya.num=cnt;
    72                     num[team[x]]=cnt;
    73                     tot[team[x]]++;
    74                 }
    75                 ya.x=x;
    76                 ya.t=step;
    77                 q.push(ya);
    78             }
    79             else
    80             {
    81                 ya=q.top();
    82                 tot[team[ya.x]]--;
    83                 q.pop();
    84                 printf("%d
    ",ya.x);
    85             }
    86         }
    87     }
    88 }
    89 
    90 int main ()
    91 {
    92     init();
    93     close();
    94     return 0;
    95 }
  • 相关阅读:
    fetch函数的使用-常见问题
    React的理解-入门指导
    Navicat for mysql 11.1.20激活
    sqlserver使用job删除过期备份文件
    sqlserver删除重复的数据
    提高code效率
    Wampserver红色橙色解决思路----端口冲突是关键
    java mvc框架系列总结ssh,ssm,servlet
    正则表达式入门案例C#
    SpringBoot开发之《兼容http和https同时访问时443端口被占用》
  • 原文地址:https://www.cnblogs.com/cssystem/p/3212506.html
Copyright © 2011-2022 走看看