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
yzs: 2020-10-02 15:23:20

@tarun_28 Rightly said!

tarun_28: 2020-06-01 13:39:52

Simplest method: Deque + flag variable

leductoan: 2020-03-04 08:28:17

con gà Bình mất 2 đấm

binh27112004: 2020-03-04 08:17:52

Toàn cũng thế

leductoan: 2020-03-04 08:16:42

Bình là một con gà

luunguyen123: 2020-01-21 02:26:41

Using dequeue for this problem. Easy AC !!

amin_amin120: 2019-09-14 23:10:16

drives me crazy , but solved eventually , no need for reversing all the queue , just using a flag to determine if that is traversed or not , good luck

Last edit: 2019-09-14 23:11:15
eloi: 2019-04-24 10:12:25

Reverse input doesn't only reverse the elements, it should reverse all operations, be careful because that costed me a few WAs

anurag_tangri: 2019-01-04 12:09:30

use deque with flag variable !

dorman8288: 2019-01-01 11:51:22

IF you are getting wrong with linked list try using the deque


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