File size: 2,365 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 *	Author:  Sohel Rana
 *	Date:    2022-05-25 21:07:34
 *	Task:    G_-_Ada_and_Queue 
**/
#include <bits/stdc++.h>
#define endl		'\n'
#define sqr(x)		(x) * (x)
#define gcd(x,y)	__gcd(x, y)
#define lcm(x,y)	((x/gcd(x,y)) * y)
#define pf(x)		push_front(x)
#define pb(x)		push_back(x)
#define eb(x)		emplace_back(x)
#define all(x)		(x).begin(), (x).end()
#define rall(x)		(x).rbegin(), (x).rend()
#define sz(x)		(int)x.size()
#define prec(x) 	fixed<<setprecision(x)
#define debug(x)	cerr<<#x<<" = "<<(x)<< endl
#define debug2(x,y)	cerr<<#x<<" = "<<(x)<<","<<#y<<" = "<<(y)<< endl
#define unsyncIO	ios_base::sync_with_stdio(false); cin.tie(nullptr)

using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;

const ld pi = acos((ld)-1);
const int mod = 1e9+7;
const ll inf = 1e18;
const ld eps = 1e-9;
const int mx = 1e5;

using namespace std;


int main() 
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    unsyncIO;
    deque<int> q;
    int flag = 0;
    int t;
    cin >> t;
    while (t--) {
        string s;
        int item;
        cin >> s;
        if (s == "toFront"){
            cin >> item;
            if (flag % 2 == 1) {
                q.push_back(item);
            } else
                q.push_front(item);
        } else if (s == "push_back") {
            cin >> item;
            if (flag % 2 == 1) {
                q.push_front(item);
            } else
                q.push_back(item);
        } else if (s == "front") {
            if (q.empty()){
                cout << "No job for Ada?" << endl;
            } else {
                if (flag % 2 == 1) {
                    cout << q.back() << endl;
                    q.pop_back();
                } else {
                    cout << q.front()<<endl;
                    q.pop_front();
                }
            }  
        } else if (s == "back") {
            if (q.empty()) {
                cout << "No job for Ada?" << endl;
            } else {
                if (flag % 2 == 1) {
                    cout << q.front() << endl;
                    q.pop_front();
                } else {
                    cout << q.back() << endl;
                    q.pop_back();
                }
            }
        } else if (s == "reverse") {
            flag++;
        }
        
    }
    return 0;
}