Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=481
Queues and Priority Queues are data structures which are known to most computer scientists. TheTeam Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of itsteammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
Finally, a list of commands follows. There are three different kinds of commands:
The input will be terminated by a value of 0 for t.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.
2 3 101 102 103 3 201 202 203 ENQUEUE 101 ENQUEUE 201 ENQUEUE 102 ENQUEUE 202 ENQUEUE 103 ENQUEUE 203 DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 2 5 259001 259002 259003 259004 259005 6 260001 260002 260003 260004 260005 260006 ENQUEUE 259001 ENQUEUE 260001 ENQUEUE 259002 ENQUEUE 259003 ENQUEUE 259004 ENQUEUE 259005 DEQUEUE DEQUEUE ENQUEUE 260002 ENQUEUE 260003 DEQUEUE DEQUEUE DEQUEUE DEQUEUE STOP 0
Scenario #1 101 102 103 201 202 203 Scenario #2 259001 259002 259003 259004 259005 260001
思路:
1. 怎么识别某个人所在team在队中的位置(想想你插队时是不是要找熟人)?
——把每个人的编号对应到一个数字(team的编号)。
2. 怎么构建这个数据结构?
——注意到无论怎么插队,每个team都是在一块的,是一个单独的queue,而整个team群是一个list(选择list是因为要从头遍历队列查找所在team,并且还有删除某个team的操作),
所以用list<queue<int> >
完整代码:
/*0.069s*/ #include<cstdio> #include<cstring> #include<list> #include<queue> using namespace std; int team[1000010]; char str[10]; list<queue<int> > L; list<queue<int> >::iterator iter; queue<int> tq; int main() { int t, n, num, cas = 0, i, j; while (scanf("%d", &t), t) { L.clear(); printf("Scenario #%d\n", ++cas); for (i = 0; i < t; ++i) { scanf("%d", &n); for (j = 0; j < n; ++j) { scanf("%d", &num); team[num] = i; } } while (scanf("%s", str), str[0] != 'S') { if (str[0] == 'E') { scanf("%d", &num); bool flag = false; for (iter = L.begin(); iter != L.end(); ++iter)///我可以插队吗? if (team[(*iter).front()] == team[num]) { (*iter).push(num); flag = true; break; } if (!flag)///unlucky... { tq.push(num); L.push_back(tq); tq.pop(); } } else { printf("%d\n", (*L.begin()).front()); (*L.begin()).pop(); if ((*L.begin()).empty()) L.pop_front(); } } putchar(10); } return 0; }