Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <stack>
  4.  
  5. int maxx(int left, int right) {
  6. if (left < right) {
  7. return right;
  8. } else {
  9. return left;
  10. }
  11. }
  12.  
  13.  
  14. class authentic_queue {
  15. public:
  16. void migrate() {
  17. while (!left.empty()) {
  18. right.push(left.top());
  19. int new_max = right.top();
  20. if (!max_right.empty()) {
  21. new_max = maxx(max_right.top(), right.top());
  22. }
  23. max_right.push(new_max);
  24.  
  25. left.pop();
  26. max_left.pop();
  27. }
  28. }
  29.  
  30. void push_new(int cannon) {
  31. left.push(cannon);
  32.  
  33. int new_max = left.top();
  34. if (!max_left.empty()) {
  35. new_max = maxx(max_left.top(), left.top());
  36. }
  37. max_left.push(new_max);
  38. }
  39.  
  40. void pop_first() {
  41. if (!right.empty()) {
  42. right.pop();
  43. max_right.pop();
  44.  
  45. } else {
  46. migrate();
  47.  
  48. if (!right.empty()) {
  49. right.pop();
  50. max_right.pop();
  51. }
  52. }
  53. }
  54.  
  55.  
  56.  
  57. int get_max() {
  58. int ans = -1e9 - 1e8;
  59. if (!max_left.empty()) {
  60. ans = maxx(ans, max_left.top());
  61. }
  62. if (!max_right.empty()) {
  63. ans = maxx(ans, max_right.top());
  64. }
  65. return ans;
  66. }
  67.  
  68. bool is_empty() { return left.empty() && right.empty(); }
  69.  
  70. private:
  71. std::stack<int> left;
  72. std::stack<int> max_left;
  73.  
  74. std::stack<int> right;
  75. std::stack<int> max_right;
  76. };
  77.  
  78.  
  79.  
  80. int main() {
  81. int len, moves, it = 0;
  82. std::cin >> len;
  83.  
  84. std::vector<int> data(len);
  85. authentic_queue tape;
  86.  
  87. for (auto& element : data) {
  88. std::cin >> element;
  89. }
  90. std::cin >> moves;
  91.  
  92. tape.push_new(data[0]);
  93.  
  94. for (int iter = 0; iter < moves; ++iter) {
  95. char command;
  96. std::cin >> command;
  97. if (command == 'L') {
  98. tape.pop_first();
  99. std::cout << tape.get_max() << " ";
  100. }
  101. if (command == 'R') {
  102. ++it;
  103. tape.push_new(data[it]);
  104. std::cout << tape.get_max() << " ";
  105. }
  106. }
  107. std::cout << std::endl;
  108.  
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement