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
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++?

nikhil_cs: 2016-12-22 17:57:27

For "TLE" :-
just think for reverse nothing else.
:)

Sushovan Sen: 2016-11-02 10:35:10

I used only array for O(1) solution.

gaurav811: 2016-10-23 14:26:06

can this be done "efficiently" without using linked list?

Last edit: 2016-10-23 21:37:28
Sushovan Sen: 2016-10-13 09:47:05

@shubham_2324: O(1) solution possible for each query.

shubham_2324: 2016-09-27 19:10:30

Thanks! i got the problem but then also i am getting time limit exeeded while i am doing all operations in O(1) time except reverse which is taking O(n) , what to do.

morass: 2016-09-27 00:47:10

@shubham_2324: Generate a few test-cases and try to debug ;-)

Your program actually failed in function "reverse" in "while" cycle where the "current" pointer was equal "0".

But no clue what caused it :)

shubham_2324: 2016-09-26 18:57:56

i was getting time limit exeeded in python so i tried it in c with a queue of circular doubly linked list,now i am getting runtime error while my code is perfectly working on ideone.please help


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