原题链接:https://vjudge.net/problem/UVA-540
分类:<queue>
备注:queue与STL其他容器的综合运用
思路
看题目标题一般都会考虑队列的,题目内容确实也是排队。
已知队伍数目上限1000,那么开数组把不同队伍分开是可以接受的。
用queue数组存取在排队的人,每次新来一个人找已用过的queue中是否有队友,有则加入队伍,无则再用一个queue存入新来的。这样每个队伍已经根据数组的下标排好了顺序,每当前面队伍的人全走完了才会找下一个队伍的人,即可符合题目要求。
代码如下:
#include<iostream>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int maxt = 1000 + 5;
int t, n, x, kase;
int main(void)
{
while (~scanf("%d", &t) && t)
{
printf("Scenario #%d\n", ++kase);
map<int, int>team;
for(int i=0;i<t;i++)
{
scanf("%d", &n);
for (int j = 0; j < n; j++)
{
scanf("%d", &x);
team[x] = i;
}
}
string cmd;
queue<int>wait[maxt];
int now = 0, cnt = 0;
while (cin >> cmd)
{
if (cmd[0] == 'S')break;
else if (cmd[0] == 'E')
{
scanf("%d", &x);
int ok = 0;
for (int i = now; i < cnt; i++)
if (!wait[i].empty() && team[wait[i].front()] == team[x])
{
wait[i].push(x);
ok = 1;
break;
}
if (!ok)wait[cnt++].push(x);
}
else if (cmd[0] == 'D')
{
if (wait[now].empty()) now++;
printf("%d\n", wait[now].front());
wait[now].pop();
}
}
printf("\n");
}
return 0;
}