UVA540 TeamQueue【map+queue】
題面:
Queues and Priority Queues are data structures which are known to most computer scientists. The
Team 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 its teammates (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.
Input
The input file will contain one or more test cases. Each test case begins with the number of teams
t (1 ≤ t ≤ 1000). Then t team descriptions follow, each one consisting of the number of elements
belonging to the team and the elements themselves. Elements are integers in the range 0..999999. A
team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
? ENQUEUE x — enter element x into the team queue
? DEQUEUE — process the first element and remove it from the queue
? STOP — end of test case
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.
Output
For each test case, first print a line saying ‘Scenario #k’, where k is the number of the test case. Then,
for each ‘DEQUEUE’ command, print the element which is dequeued on a single line. Print a blank line
after each test case, even after the last one.
Sample Input
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
Sample Output
Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001
題目大意:
現在需要構建一個特殊的隊列,這個隊列的特點是當一個元素入隊時,先在隊列裏看是否有這個元素的隊友,如果有就將這個元素放在最後一個隊友的後面,如果沒有就放在隊尾。要求有較低的時間復雜度。
大致思路:
可以利用map將一個隊中的所有元素標記。並且每一隊都有一個專門的隊列,用來放入隊的元素。還有一個主隊列,裏面放的都是不同隊的元素。
當入隊時,先根據對應的map值看其專門隊裏是否為空,如果不為空就入其專門的隊列,如果為空不光入專門的隊,還要入主隊。
出隊時,從專門的隊中出元素,直到專門的隊空時,才從主隊中將這個元素剔除。
代碼:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 ios::sync_with_stdio(false); 6 //freopen("in.txt","r",stdin); 7 char str[50]; 8 int t,n,x,cnt=1; 9 while(cin>>t&&t) 10 { 11 map<int,int> mp; 12 queue<int> ma,team[1010];//主隊和專門的隊 13 for(int i=0;i<t;++i){ 14 cin>>n; 15 for(int j=0;j<n;++j){ 16 cin>>x; 17 mp[x]=i; 18 } 19 } 20 cout<<"Scenario #"<<cnt<<endl; 21 while(1) 22 { 23 cin>>str; 24 if(str[0]==‘S‘) 25 break; 26 if(str[0]==‘E‘){ 27 cin>>x; 28 if(team[mp[x]].empty())//看專門的隊是否為空 29 ma.push(x); 30 team[mp[x]].push(x); 31 }else{ 32 x=ma.front(); 33 cout<<team[mp[x]].front()<<endl; 34 team[mp[x]].pop(); 35 if(team[mp[x]].empty())//專門隊為空再出主隊 36 ma.pop(); 37 } 38 } 39 cnt++; 40 cout<<endl; 41 } 42 return 0; 43 }
UVA540 TeamQueue【map+queue】