题意大概是这样的.
一个人买饭的时候要排队, 有N个小队. 如果这个人有队友在前面排队, 可以直接插到队友的后面. 不然只能排在队伍的最后方.
输出按队伍的顺序输出.
一开始没思路, 参考了很多解题报告也不懂. 直到看到synapse7的解题报告, 引用一下他的思路.
思路:
1. 怎么识别某个人所在team在队中的位置(想想你插队时是不是要找熟人)?
——把每个人的编号对应到一个数字(team的编号)。
2. 怎么构建这个数据结构?
——注意到无论怎么插队,每个team都是在一块的,是一个单独的queue,而整个team群是一个list(选择list是因为要从头遍历队列查找所在team,并且还有删除某个team的操作),
所以用list<queue<int> >
非常好的思路, 非常好的表达...非常佩服....非常感谢...
再一次被STL的强大震撼. 暑假有空一定要读一读STL的源码.
详情见代码
#include <cstdio>
#include <list>
#include <queue>
using namespace std;
int num[1000000];
int main()
{
//freopen("input.txt", "r", stdin);
int cnt = 1;
list<queue<int> > li;
list<queue<int> >::iterator it;
queue<int> qu;
int t, n, i, j, temp, peo;
char str[100];
bool flag;
while (scanf("%d", &t) && t)
{
li.clear(); //记得清空历史遗留问题.
printf("Scenario #%d\n", cnt++);
for (i = 0; i < t; i++)
{
scanf("%d", &n);
for (j = 0; j < n; j++) //先分好组.
{
scanf("%d", &temp);
num[temp] = i;
}
}
while (scanf("%s", str))
{
if (str[0] == 'S')
break;
else if (str[0] == 'E') //先查找有没有熟人在队里
{
scanf("%d", &peo);
flag = false;
for (it = li.begin(); it != li.end(); it++)
{
if (num[it->front()] == num[peo]) //有熟人, 可以插队.
{
it->push(peo);
flag = true;
break;
}
}
if (!flag) //没熟人, 苦逼排在最后面.
{
qu.push(peo);
li.push_back(qu);
qu.pop();
}
}
else if (str[0] == 'D')
{
printf("%d\n", li.begin()->front());
li.begin()->pop();
if (li.begin()->empty())
li.pop_front();
}
else
break;
}
printf("\n");
}
return 0;
}