zoukankan      html  css  js  c++  java
  • queue POJ 2259 Team Queue

    题目传送门

    题意:先给出一些小组成员,然后开始排队。若前面的人中有相同小组的人的话,直接插队排在同小组的最后一个,否则只能排在最后面。现在有排队和出队的操作。

    分析:这题关键是将队列按照组数分组,用另外一个队列保存组的序号,当该组里没有人了才换下一组。很好的一道题。

    收获:队列的灵活运用

    代码:

    /************************************************
    * Author        :Running_Time
    * Created Time  :2015/9/9 星期三 14:37:14
    * File Name     :M.cpp
     ************************************************/
    
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <sstream>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <list>
    #include <map>
    #include <set>
    #include <bitset>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int N = 1e3 + 10;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    queue<int> id;
    queue<int> num[N];
    map<int, int> mp;
    bool inq[N];
    char op[11];
    int n;
    
    void init(void) {
        while (!id.empty ())    id.pop ();
        for (int i=1; i<=n; ++i)    {
            while (!num[i].empty ())   num[i].pop ();
        }
        memset (inq, false, sizeof (inq));
        mp.clear ();
    }
    
    int main(void)    {
        int cas = 0;
        while (scanf ("%d", &n) == 1)   {
            if (!n) break;
            init ();
            if (cas)    puts ("");
            printf ("Scenario #%d
    ", ++cas);
            int x;
            for (int i=1; i<=n; ++i)    {
                int m;  scanf ("%d", &m);
                for (int j=1; j<=m; ++j)    {
                    scanf ("%d", &x);
                    mp[x] = i;
                }
            }
            scanf ("%s", &op);
            while (strcmp (op, "STOP")) {
                if (strcmp (op, "ENQUEUE") == 0)    {
                    scanf ("%d", &x);
                    if (!inq[mp[x]])    {
                        inq[mp[x]] = true;
                        id.push (mp[x]);
                    }
                    num[mp[x]].push (x);
                }
                else    {
                    if (id.empty ())    continue;
                    int fir = id.front ();
                    printf ("%d
    ", num[fir].front ()); num[fir].pop ();
                    if (num[fir].empty ())  {
                        inq[fir] = false;   id.pop ();
                    }
                }
                scanf ("%s", &op);
            }
        }
    
        return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    在mvc4中上传、导入和导出excel表方法总结
    ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
    .net下使用NPOI读取Excel表数据
    详解免费高效实用的.NET操作Excel组件NPOI(转)
    Vue 定时执行函数
    decorators.xml的用法
    行内元素和块级元素
    C/C++中printf和C++中cout的输出格式
    IIS 搭建过程
    IIS访问共享文件详解
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4795710.html
Copyright © 2011-2022 走看看