zoukankan      html  css  js  c++  java
  • ACM题目————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 ( ). 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

    题意:有n个队伍。 对于每个ENQUEUE  x 命令。 如果x所在的队伍已经在队列中, 则x排在队列中它的队伍的尾巴, 否则排在队列的末尾。 可以理解为队列中的队列的味道。

    这题有点蒙,但是弄懂后,感觉自己对队列似乎了解深了许多啊!

    //Asimple
    //#include <bits/stdc++.h>
    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <deque>
    #include <string>
    #include <string.h>
    #include <vector>
    #include <stack>
    #include <ctype.h>
    #include <queue>
    #include <math.h>
    #include <stdlib.h>
    #include <map>
    #include <set>
    #include <time.h>
    #include <bitset>
    #include <list>
    
    using namespace std;
    #define INF 0xFFFFFFFF
    typedef long long ll ;
    typedef list<int>::iterator l_iter;
    typedef multimap<string,string>::iterator mss_iter;
    typedef map<string,string>::iterator m_iter;
    typedef set<int>::iterator s_iter;
    typedef vector<int>::iterator v_iter;
    multimap<string,string> mss;
    const int maxn = 1000;
    const int Max = 1000000;
    int n, T, num, cnt;
    string str;
    stack<set<int> > stk;
    map<set<int>, int> m;
    set<int> s1, s2;
    queue<int> q;
    queue<int> qq[maxn];
    list<int> L;
    int a[Max];
    
    int main()
    {
        int k = 1 ;
        while( cin >> T && T )
        {
            while( !q.empty() ) q.pop();
            for(int i=0; i<maxn; i++)
                while( !qq[i].empty() )
                    qq[i].pop();
            memset(a,0,sizeof(a));
            //入队
            for(int i=0; scanf("%d",&n)==1; i++)
            {
                for(int j=0; j<n; j++)
                {
                    scanf("%d%*c",&num);
                    a[num] = i ;
                }
            }
    
            cout << "Scenario #" << k  << endl ;
            while(true)
            {
                cin >> str ;
                if( str == "STOP" )
                {
                    cout << endl ;
                    break;
                }
                if( str == "ENQUEUE" )
                {
                    scanf("%d%*c",&num);
                    if( qq[a[num]].empty() )
                        q.push(a[num]);
                    qq[a[num]].push(num);
                }
                if( str == "DEQUEUE" )
                {
                    int q_num = q.front();
                    cout << qq[q_num].front() << endl ;
                    qq[q_num].pop();
                    if( qq[q_num].empty() )
                        q.pop();
                }
            }
    
            k ++ ;
        }
    
        return 0;
    }
    
    低调做人,高调做事。
  • 相关阅读:
    rpc的几种服务端模型分析
    ASP.NET MVC3在Visual Studio 2010中的变化
    HTML元素的ID和Name属性的区别
    Oracle日期类操作(格式 加减乘 取毫秒)
    快速原型工具
    C#4.0新特性dynamic\可选参数、命名参数
    WCF注册Windows Service
    不要在using语句中调用WCF服务
    C# 4.0 新特性dynamic、可选参数、命名参数等
    Windows内置系统账户Local system/Network service/Local Service
  • 原文地址:https://www.cnblogs.com/Asimple/p/5526971.html
Copyright © 2011-2022 走看看