UVA540 Team Queue

链接:https://vjudge.net/problem/UVA-540

        队列是符合“先进先出”(First In First Out, FIFO)原则的“公平队列“。STL队列定义在头文件<queue>中,可以用”queue<int> s“方式声明一个队列,用push()和pop()进行元素的入队和出队操作,front()取队首元素(但不删除)。

#include <iostream>
#include <queue>
#include <map>
#include <cstdio>
using namespace std;

const int maxt=1000+10;

int main()
{
    int t,kase=0;
    while(scanf("%d",&t)==1&&t)
    {
        printf("Scenario #%d\n",++kase);

        //记录所有人的团队编号
        map<int,int> team;//team[x]表示编号为x的人所在的团队编号
        for(int i=0;i<t;i++)
        {
            int n,x;
            scanf("%d",&n);
            while(n--)
            {
                scanf("%d",&x);
                team[x]=i;
            }
        }

        //模拟
        queue<int> q,q2[maxt];
        for(;;)
        {
            int x;
            char cmd[10];
            scanf("%s",cmd);
            if(cmd[0]=='S')
            {
                break;
            }
            else if(cmd[0]=='D')
            {
                int t=q.front();
                printf("%d\n",q2[t].front());
                q2[t].pop();
                if(q2[t].empty())//团队t空了,全体出队列
                    q.pop();
            }
            else if(cmd[0]=='E')
            {
                scanf("%d",&x);
                int t=team[x];
                if(q2[t].empty())
                    q.push(t);//团队t进入队列
                q2[t].push(x);
            }
        }
        printf("\n");
    }
    return 0;
}
AC Code

本来写了一些队列的常规操作在台式机上,结果机房的交换机被人弄走了23333,现在用自己的笔记本写了这篇博客,大家可以自行查一下队列,在cplusplus上面其实讲的很清楚。

posted @ 2018-08-31 16:31  子诚-  阅读(119)  评论(0编辑  收藏  举报