Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class deque
  4. {
  5. int buffersize = 4;
  6. int* buffer;
  7. int back = 2;
  8. int front = 1;
  9. int amount = 0;
  10. public:
  11. void grow ()
  12. {
  13. if (back == buffersize - 1)
  14. grow_back ();
  15. if (front == 0)
  16. grow_front ();
  17. }
  18. void grow_back ()
  19. {
  20. /*
  21. int newbuffersize = buffersize * 2;
  22. int* newBuffer = new int [newbuffersize];
  23. for (int i = 0; i < buffersize; i++)
  24. newBuffer [i] = buffer [i];
  25. delete[] buffer;
  26. buffersize = newbuffersize;
  27. buffer = newBuffer;
  28. */
  29. buffersize *= 2;
  30. }
  31. void grow_front ()
  32. {
  33. /*
  34. int newbuffersize = buffersize * 2;
  35. int* newBuffer = new int [newbuffersize];
  36. for (int i = newbuffersize / 2; i < newbuffersize ; i++)
  37. newBuffer [i] = buffer [i];
  38. front = buffersize;
  39. back += buffersize;
  40. delete[] buffer;
  41. buffersize = newbuffersize;
  42. buffer = newBuffer;
  43. */
  44. int reserve = buffersize;
  45. buffersize *= 2;
  46. for (int i = 0; i < reserve; i++)
  47. buffer [i + reserve] = buffer [i];
  48. front += reserve;
  49. back += reserve;
  50. }
  51. void push_back (int a)
  52. {
  53. grow ();
  54. buffer [back] = a;
  55. back++;
  56. amount++;
  57. }
  58. void push_front (int a)
  59. {
  60. grow ();
  61. buffer [front] = a;
  62. front--;
  63. amount++;
  64. }
  65. void pop_back ()
  66. {
  67. back--;
  68. amount--;
  69. }
  70. void pop_front ()
  71. {
  72. front++;
  73. amount--;
  74. }
  75. int elem_back ()
  76. {
  77. return buffer [back - 1];
  78. }
  79. int elem_front ()
  80. {
  81. return buffer [front + 1];
  82. }
  83. int count ()
  84. {
  85. return amount;
  86. }
  87. };
  88. int main ()
  89. {
  90. int n = 0;
  91. cin >> n;
  92. deque arr;
  93. string truth = "YES";
  94. for (int i = 0; i < n; i++)
  95. {
  96. int a = 0;
  97. int b = 0;
  98. cin >> a >> b;
  99. if (a == 1)
  100. arr.push_front (b);
  101. if (a == 2)
  102. {
  103. if (b != arr.elem_front () or (arr.count () == 0 and b != -1))
  104. truth = "NO";
  105. else
  106. arr.pop_front ();
  107. }
  108. if (a == 3)
  109. arr.push_back (b);
  110. if (a == 4)
  111. {
  112. if (b != arr.elem_back () or (arr.count () == 0 and b != -1))
  113. truth = "NO";
  114. else
  115. arr.pop_back ();
  116. }
  117. }
  118. cout << truth << "\n";
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement