ADAQUEUE - Ada and Queue


Ada the Ladybug has many things to do. She puts them into her queue. Anyway she is very indecisive, so sometime she uses the top, sometime the back and sometime she decides to reverses it.

Input

The first line consists of 1 ≤ Q ≤ 106, number of queries. Each of them contains one of following commands

back - Print number from back and then erase it

front - Print number from front and then erase it

reverse - Reverses all elements in queue

push_back N - Add element N to back

toFront N - Put element N to front

All numbers will be 0 ≤ N ≤ 100

Output

For each back/front query print appropriate number.

If you would get this type of query and the queue would be empty, print "No job for Ada?" instead.

Example Input

15
toFront 93
front
back
reverse
back
reverse
toFront 80
push_back 53
push_back 50
front
front
reverse
push_back 66
reverse
front

Example Output

93
No job for Ada?
No job for Ada?
80
53
66

hide comments
undefine: 2018-09-04 11:14:45

*try STL
*avoid O(n^2)

avik26091998: 2017-12-04 17:32:30

Used Double Linked List...0.16s...Lot of Typing...

thalysmg: 2017-11-09 21:33:12

I'm getting WA in python, dont know y. I cant find a test case which breaks my code.

Last edit: 2017-11-09 21:34:27
saltyfish233: 2017-10-26 08:45:33

Easy problem. AC in one go. Just remember you don`t need to reverse your queue actually.

nadstratosfer: 2017-10-18 02:26:37

Solvable in Python, not a given with a Morass' problem. If TLE, make it your Data Structures Awareness Day.

morass: 2017-06-22 13:36:09

@aditya_rev: imho it is deleting first element of vector ^_^

aditya_rev: 2017-06-21 19:13:02

what? how can i got tle?:/ no need O(n) to reverse.

vanvinhbk94: 2017-02-28 16:30:59

Final AC.nice problem :))

morass: 2017-02-25 17:50:48

@kasiekdec: I think there shall be no problem with speed of IO. Anyway your algorithm seems to work in O(N^2) which is too much for this problem - so I guess you shall investigate this :)

Have Nice Day!

kasiekdec: 2017-02-25 15:29:21

I am getting runtime error in c++... I tried to use scanf for reading input, but I cannot check input (e.g. if there is nothing to be put in a queue like 'reverse'/'back'/'front'. Could this be solved with streams (cin?)? Any hints for c++?


Added by:Morass
Date:2016-09-06
Time limit:3.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU