Team Queue
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 3282 |
|
Accepted: 1188 |
Description
Queues and priority Queues is data structures which is known to most computer scientists. The Team Queue, however, isn't so well known, though it occurs often in everyday life. At lunch time the "queue in front of the Mensa are 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) is 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 do like in normal queues:elements be processed from head to tail in the order they appear in the team que Ue.
Your task is to write a program that simulates such a team queue.
Input
The input would contain one or more test cases. Each test case is 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 them Selves. Elements is integers in the range 0-999999. A team may consist of up to elements.
Finally, a list of commands follows. There is 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 is terminated by a value of 0 for T.
WARNING:A test case could contain up to 200000 (both hundred thousand) commands, so the implementation of the team queue Sho Uld is 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 was the number of the the test case. Then, for each DEQUEUE command, print the element which was dequeued on a. Print a blank line after all test case, even after the last one.
Sample Input
101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUED EQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260 001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003dequeuedequeuedequeuedequeuestop0
Sample Output
Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001
Source
ULM Local 1998
Accode:
<pre class= "Sh_cpp sh_sourcecode" style= "Font-family:courier new,courier,monospace" ><span class= "Sh_ Preproc "> #include </span> <span class=" sh_string "><iostream></span><span class=" Sh_ Preproc "> #include </span> <span class=" sh_string "><cstdio></span><span class=" Sh_ Preproc "> #include </span> <span class=" sh_string "><cstring></span><span class=" Sh_ Keyword ">using</span> <span class=" Sh_keyword ">namespace</span> std<span class=" Sh_symbol " >;</span><span class= "Sh_keyword" >struct</span><span class= "Sh_normal" > </span> <span class= "Sh_classname" >node</span><span class= "Sh_cbracket" >{</span> <span class= " Sh_type ">int</span> p<span class=" Sh_symbol ">;</span> <span class=" Sh_type ">int</ span> next<span class= "Sh_symbol" >;</span><span class= "Sh_cbracket" >}</span> My<span class= "Sh_symbol" >[</span><span class= "Sh_number" >200020</span><span class= " Sh_symbol ">];</span><span class=" Sh_type ">int</span> belong<span class=" Sh_symbol ">[ </span><span class= "Sh_number" >1000000</span><span class= "Sh_symbol" >];</span>< Span class= "sh_comment" >///hashing indicates which queue the element belongs to </span><span class= "Sh_type" >int</span> Pos<span class= "Sh_symbol" >[</span><span class= "Sh_number" >1010</span><span class= "Sh_symbol" > ];</span><span class= "Sh_comment" >///team where the last element is in the queue </span><span class= "Sh_type" >int< /SPAN> st<span class= "Sh_symbol" >,</span>ed<span class= "Sh_symbol" >,</span>used< Span class= "Sh_symbol" >;</span><span class= "Sh_type" >int</span> <span class= "Sh_function" >main</span><span class= "Sh_symbol" > () </span><span class= "Sh_cbracket" >{</span> <span class= "Sh_type" >int</span> loop<span class= "Sh_symbol" >=</span><span class = "Sh_number" >0</span><span class= "Sh_symbol" >,</span>n<span class= "Sh_symbol" >;</ span> <span class= "Sh_keyword" >while</span><span class= "Sh_symbol" > (</span><span class= "Sh_function" >scanf</span><span class= "Sh_symbol" > (</span><span class= "sh_string" > "%d" </span><span class= "Sh_symbol" >,&</span>n<span class= "Sh_symbol" >)!=</ Span>eof<span class= "Sh_symbol" >&&</span>n<span class= "Sh_symbol" >) </span> <span class= "Sh_cbracket" >{</span> st<span class= "Sh_symbol" >=</span>ed<span class= "Sh_ Symbol ">=-</span><span class=" Sh_number ">1</span><span class=" Sh_symbol ">;</span > Used<span class= "sh_symbol" >=</span><span class= "Sh_number" >0</sPan><span class= "Sh_symbol" >;</span> <span class= "Sh_type" >int</span> Num<span class= "Sh_symbol" >,</span>temp<span class= "Sh_symbol" >;</span> <span class= "Sh_keyword" >for& Lt;/span><span class= "Sh_symbol" > (</span><span class= "Sh_type" >int</span> I<span class= "Sh_symbol" >=</span><span class= "Sh_number" >0</span><span class= "Sh_symbol" >; </span>i<span class= "Sh_symbol" ><</span>n<span class= "Sh_symbol" >;++</span>i <span class= "Sh_symbol" >) </span><span class= "Sh_cbracket" >{</span> <span class= "Sh_f Unction ">scanf</span><span class=" Sh_symbol "> (</span><span class=" sh_string ">"%d "< /span><span class= "Sh_symbol" >,&</span>num<span class= "Sh_symbol" >);</span> < Span class= "Sh_keyword" >for</span><span class= "Sh_symbol";(</span><span class= "Sh_type" >int</span> j<span class= "Sh_symbol" >=</span><span class= "Sh_number" >0</span><span class= "Sh_symbol" >;</span>j<span class= "Sh_symbol" > <</span>num<span class= "Sh_symbol" >;++</span>j<span class= "Sh_symbol" >) </span> <span class= "Sh_cbracket" >{</span> cin<span class= "Sh_symbol" >>></SPAN>TEMP&L T;span class= "Sh_symbol" >;</span> belong<span class= "Sh_symbol" >[</span>temp<span C lass= "Sh_symbol" >]=</span>i<span class= "Sh_symbol" >;</span> <span class= "Sh_cbracket" &G t;} </span> pos<span class= "Sh_symbol" >[</span>i<span class= "Sh_symbol" >]=-</span>< Span class= "Sh_number" >1</span><span class= "Sh_symbol" >;</span> <span class= "Sh_cbracket" >}</span> <span class= "Sh_keyword">if</span><span class= "Sh_symbol" > (</span>loop<span class= "Sh_symbol" >) </span> <span class= "sh_function" >putchar</span><span class= "Sh_symbol" > (</span><span class= " Sh_string "> </span><span class=" Sh_specialchar ">\n</span><span class=" sh_string ">" </span><span class= "Sh_symbol" >);</span> <span class= "Sh_function" >printf</span> <span class= "Sh_symbol" > (</span><span class= "sh_string" > "Scenario #%d</span><span Class = "Sh_specialchar" >\n</span><span class= "sh_string" > "</span><span class=" Sh_symbol ">,+ +</span>loop<span class= "Sh_symbol" >);</span> string s<span class= "Sh_symbol" >;</SPAN&G T <span class= "Sh_keyword" >while</span><span class= "Sh_symbol" > (</span>cin<span class= " Sh_symbol ">>></span>s<span class=" Sh_symbol ">&&</sPan>s<span class= "Sh_symbol" >!=</span><span class= "sh_string" > "STOP" </span><span class= "Sh_symbol" >) </span><span class= "Sh_cbracket" >{</span> <span class= "Sh_keyword" &G T;if</span><span class= "Sh_symbol" > (</span>s<span class= "Sh_symbol" >==</span>< Span class= "sh_string" > "ENQUEUE" </span><span class= "Sh_symbol" >) </span><span class= "Sh_ Cbracket ">{</span> <span class=" sh_function ">scanf</span><span class=" Sh_symbol " ;(</span><span class= "sh_string" > "%d" </span><span class= "Sh_symbol" >,&</span> Temp<span class= "Sh_symbol" >);</span> my<span class= "Sh_symbol" >[</span>used<spa n class= "Sh_symbol" >].</span>p<span class= "Sh_symbol" >=</span>temp<span class= "Sh_symbol" >;</span> <span class= "Sh_keyword" >if</span≫<span class= "Sh_symbol" > (</span>pos<span class= "Sh_symbol" >[</span>belong<span class= "Sh_symbol" >[</span>temp<span class= "Sh_symbol" >]]==-</span><span class= "Sh_number ">1</span><span class=" Sh_symbol ">) </span><span class=" Sh_cbracket ">{</span> My<span class= "Sh_symbol" >[</span>used<span class= "Sh_symbol" >].</span>next<span class = "Sh_symbol" >=-</span><span class= "Sh_number" >1</span><span class= "Sh_symbol" >;</ span> <span class= "Sh_keyword" >if</span><span class= "Sh_symbol" > (</SPAN>ST&L T;span class= "Sh_symbol" >==-</span><span class= "Sh_number" >1</span><span class= "Sh_symbol ">&&</span>ed<span class=" Sh_symbol ">==-</span><span class=" Sh_number ">1</ Span><span class= "Sh_symbol" >) </span><span class= "Sh_cbracket">{</span> st<span class=" Sh_symbol ">=</span>ed<span class=" Sh_symbol ">= </span><span class= "Sh_number" >0</span><span class= "Sh_symbol" >;</span> & Lt;span class= "Sh_cbracket" >}</span> <span class= "Sh_keyword" >if</span><span cl ass= "Sh_symbol" > (</span>ed<span class= "Sh_symbol" >>=</span><span class= "Sh_number" >0</span><span class= "Sh_symbol" >) </span><span class= "Sh_cbracket" >{</span> My<span class= "Sh_symbol" >[</span>ed<span class= "Sh_symbol" >].</span>next<span class= "Sh_symbol" >=</span>used<span class= "Sh_symbol" >;</span> <span class= "Sh_cbrack ET ">}</span> ed<span class=" Sh_symbol ">=</span>used<span class=" Sh_symbol ">; </span> <span class= "Sh_cbracket" >}</span><span class= "Sh_keyword" >else</span> <span class= "Sh_cbracket ">{</span> <span class=" Sh_keyword ">if</span><span class=" Sh_symbol "> (</s Pan>pos<span class= "Sh_symbol" >[</span>belong<span class= "Sh_symbol" >[</span>temp< Span class= "Sh_symbol" >]]==</span>ed<span class= "Sh_symbol" >) </span>ed<span class= "Sh_ Symbol ">=</span>used<span class=" Sh_symbol ">;</span> my<span class=" Sh_symbol "& Gt [</span>used<span class= "Sh_symbol" >].</span>next<span class= "Sh_symbol" >=</span> My<span class= "Sh_symbol" >[</span>pos<span class= "Sh_symbol" >[</span>belong<span class = "Sh_symbol" >[</span>temp<span class= "Sh_symbol" >]]].</span>next<span class= "Sh_symbol" >;</span> my<span class= "Sh_symbol" >[</SPAN>pos<span class= "Sh_symbol" >[</span>belong<span class= "Sh_symbol" >[</span>temp<span class= "Sh_symbol" >]]].</span>next<span class= "Sh_symbol" >=</span>used<span class= "Sh_ Symbol ">;</span> <span class=" Sh_cbracket ">}</span> pos<span class=" Sh_symbol ">[</span>belong<span class=" Sh_symbol ">[</span>temp<span class=" Sh_symbol "> ]]=</span>used<span class= "Sh_symbol" >++;</span> <span class= "Sh_cbracket" >}</SPAN&G T;<span class= "Sh_keyword" >else</span> <span class= "Sh_cbracket" >{</span> <span class= "Sh_function" >printf</span><span class= "Sh_symbol" > (</span><span class= "sh_string" > "%d</span><span class=" Sh_specialchar ">\n</span><span class=" sh_string ">" </span ><span class= "Sh_symbol" >,</span>my<span class= "Sh_symbol"; [</span>st<span class= "Sh_symbol" >].</span>p<span class= "Sh_symbol" >);</span> <span class= "Sh_keyword" >if</span><span class= "Sh_symbol" > (</span>pos<span class= "Sh_ Symbol ">[</span>belong<span class=" Sh_symbol ">[</span>my<span class=" Sh_symbol ">[< /span>st<span class= "Sh_symbol" >].</span>p<span class= "Sh_symbol" >]]==</span>st< Span class= "Sh_symbol" >) </span> pos<span class= "Sh_symbol" >[</span>belong<span class= "Sh_symbol" >[</span>my<span class= "Sh_symbol" >[</span>st<span class= "Sh_symbol" >].</span>p<span class= "Sh_symbol" >]]=-</span><span class= "Sh_number" >1</span> <span class= "Sh_symbol" >;</span> st<span class= "Sh_symbol" >=</span>my<span class = "Sh_symbol" >[</span>st<span class= "Sh_symbol" >].</span>nExt<span class= "Sh_symbol" >;</span> <span class= "Sh_cbracket" >}</span> <span class= "Sh_cbracket" >}</span> <span class= "Sh_cbracket" >}</span> retrun 0;<span class= "Sh_ Cbracket ">}</span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2259 Team queue data structure queues