Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class deque
- {
- int buffersize = 4;
- int* buffer;
- int back = 2;
- int front = 1;
- int amount = 0;
- public:
- void grow ()
- {
- if (back == buffersize - 1)
- grow_back ();
- if (front == 0)
- grow_front ();
- }
- void grow_back ()
- {
- /*
- int newbuffersize = buffersize * 2;
- int* newBuffer = new int [newbuffersize];
- for (int i = 0; i < buffersize; i++)
- newBuffer [i] = buffer [i];
- delete[] buffer;
- buffersize = newbuffersize;
- buffer = newBuffer;
- */
- buffersize *= 2;
- }
- void grow_front ()
- {
- /*
- int newbuffersize = buffersize * 2;
- int* newBuffer = new int [newbuffersize];
- for (int i = newbuffersize / 2; i < newbuffersize ; i++)
- newBuffer [i] = buffer [i];
- front = buffersize;
- back += buffersize;
- delete[] buffer;
- buffersize = newbuffersize;
- buffer = newBuffer;
- */
- int reserve = buffersize;
- buffersize *= 2;
- for (int i = 0; i < reserve; i++)
- buffer [i + reserve] = buffer [i];
- front += reserve;
- back += reserve;
- }
- void push_back (int a)
- {
- grow ();
- buffer [back] = a;
- back++;
- amount++;
- }
- void push_front (int a)
- {
- grow ();
- buffer [front] = a;
- front--;
- amount++;
- }
- void pop_back ()
- {
- back--;
- amount--;
- }
- void pop_front ()
- {
- front++;
- amount--;
- }
- int elem_back ()
- {
- return buffer [back - 1];
- }
- int elem_front ()
- {
- return buffer [front + 1];
- }
- int count ()
- {
- return amount;
- }
- };
- int main ()
- {
- int n = 0;
- cin >> n;
- deque arr;
- string truth = "YES";
- for (int i = 0; i < n; i++)
- {
- int a = 0;
- int b = 0;
- cin >> a >> b;
- if (a == 1)
- arr.push_front (b);
- if (a == 2)
- {
- if (b != arr.elem_front () or (arr.count () == 0 and b != -1))
- truth = "NO";
- else
- arr.pop_front ();
- }
- if (a == 3)
- arr.push_back (b);
- if (a == 4)
- {
- if (b != arr.elem_back () or (arr.count () == 0 and b != -1))
- truth = "NO";
- else
- arr.pop_back ();
- }
- }
- cout << truth << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement