Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct deque1
  5. {
  6. int s=4;
  7. int *arr1=new int[s];
  8.  
  9. int index1st=0;
  10. int indexlast=s;
  11. int currentsize=0;
  12.  
  13. void increasesize()
  14. {
  15. s*=2;
  16. int *arr2 = new int[s];
  17. for (int i = 0;i <= index1st;i++)
  18. {arr2[i]=arr1[i];}
  19. for (int i=s-1;i>=indexlast+(s/2);i--)
  20. {
  21. arr2[i]=arr1[i+s/2];
  22. }
  23. indexlast += s/2;
  24. arr2=arr1;
  25. delete[] arr1;
  26.  
  27. }
  28. void pushback(int x)
  29. {
  30.  
  31. currentsize++;
  32. if (currentsize>=s)
  33. {
  34. increasesize();
  35. }
  36. arr1[indexlast]=x;
  37. indexlast--;
  38.  
  39.  
  40. }
  41. void pushfront(int x)
  42. {
  43. currentsize++;
  44. if (currentsize>=s)
  45. {
  46. increasesize();
  47. }
  48.  
  49. arr1[index1st]=x;
  50. index1st++;
  51.  
  52. }
  53. int popfront()
  54. {
  55. if ((currentsize<=0))
  56. {
  57. return -1;
  58. }
  59. if (index1st<0)
  60. {
  61. index1st+=(s-1);
  62. }
  63. currentsize--;
  64. index1st--;
  65. return arr1[index1st];
  66. }
  67. int popback()
  68. {
  69. if ((currentsize<=0))
  70. {
  71. return -1;
  72. }
  73. if (indexlast>=s)
  74. {
  75. indexlast-=(s-1);
  76. }
  77. currentsize--;
  78. indexlast++;
  79. return arr1[indexlast];
  80.  
  81. }
  82.  
  83.  
  84. };
  85.  
  86. int main()
  87. {
  88. int n;
  89. cin>>n;
  90. deque1 deque2;
  91. int command,number;
  92. int result;
  93. for (int i=0;i<n;i++)
  94. {cin>>command>>number;
  95. if (command==1)
  96. {
  97. deque2.pushfront(number);
  98. }
  99. if (command==2)
  100. {
  101. if (deque2.popfront()!= number)
  102. {cout<<"NO";
  103. break;}
  104.  
  105. }
  106. if (command==3)
  107. {
  108. deque2.pushback(number);
  109. }
  110. if (command==4)
  111. {
  112. if (deque2.popback()!=number)
  113. {cout<<"NO";
  114. break;}}
  115. if (i==n-1)
  116. {
  117. cout<<"YES";
  118. }
  119. }
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement