queuestack
InterviewTime limit1sMemory limit1024 MB
Each of N positions is a queue or stack holding one value; insert M values one at a time and print the value that exits the end of the line.
- Level
Medium5 of 10
- Topics
- Queue, Stack, Implementation, Simulation
- Solved
- No attempts yet
Problem
Dohyeon, idly enjoying his vacation, suddenly came up with an interesting data structure. He named it queuestack.
The structure of queuestack is as follows. Data structures numbered , , ..., (each a queue or a stack) are lined up, and each data structure holds one element.
queuestack works as follows.
- Read .
- Insert into data structure , then pop an element from data structure . Call the popped element .
- Insert into data structure , then pop an element from data structure . Call the popped element .
- ...
- Insert into data structure , then pop an element from data structure . Call the popped element .
- Return .
Dohyeon takes a sequence of length and inserts its elements into queuestack one by one from front to back. The results of previous insertions remain. (See Sample .)
Given the elements to insert into queuestack, write a program that outputs the return value for each insertion.
Input
The first line gives the number of data structures that make up queuestack. ()
The second line gives a sequence of length . If data structure is a queue then , and if it is a stack then .
The third line gives a sequence of length . is the element held in data structure . ()
The fourth line gives the length of the sequence to insert. ()
The fifth line gives a sequence of length containing the elements to insert into queuestack. ()
All numbers in the input are integers.
Output
For each element of sequence inserted into queuestack in order, output the return value, separated by spaces.