POJ2259,luoguUVA540-Team Queue【队列】

正题

POJ题目链接:http://poj.org/problem?id=2259
luogu评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=UVA540


题目大意

有n个队伍排队,新进来一个人时那个人会排在他的队伍的最后一个人的后面,如果这里没有他队伍的人他就只能排在最后面。
有两个操作,一个是一个人加入,一个是退出队首
求每次退出退出的人


code

#include<cstdio>
#include<queue>
#include<cstring>
#define TN 1010
using namespace std;
queue<int> tq,q[TN];
int k,t,n,x,inq[TN],team[1000010];
char s[11];
int main()
{
    k=0;
    while('.'=='.')
    {
        memset(inq,0,sizeof(inq));
        k++;
        scanf("%d",&t);
        if (!t) break;
        for(int i=1;i<=t;i++)
        {
          scanf("%d",&n);
          for(int j=1;j<=n;j++)
            scanf("%d",&x),team[x]=i;
          while(!q[i].empty())q[i].pop();
        }
        while(!tq.empty())tq.pop();
        printf("Scenario #%d\n",k);
        while(true)
        {
            scanf("%s",s);
            if(s[0]=='S') break;
            else if(s[0]=='E'){
                scanf("%d",&x);
                q[team[x]].push(x);
                if(!inq[team[x]])//该队第一人
                    tq.push(team[x]);
                inq[team[x]]++;//加入自己队列
            }
            else{
                x=tq.front();
                inq[x]--;//退出
                if(!inq[x]) tq.pop();//队伍不在当前队列里了
                printf("%d\n",q[x].front());//输出
                q[x].pop();
            }
        }
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/Mr_wuyongcong/article/details/81706145