Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <stack>
- int maxx(int left, int right) {
- if (left < right) {
- return right;
- } else {
- return left;
- }
- }
- class authentic_queue {
- public:
- void migrate() {
- while (!left.empty()) {
- right.push(left.top());
- int new_max = right.top();
- if (!max_right.empty()) {
- new_max = maxx(max_right.top(), right.top());
- }
- max_right.push(new_max);
- left.pop();
- max_left.pop();
- }
- }
- void push_new(int cannon) {
- left.push(cannon);
- int new_max = left.top();
- if (!max_left.empty()) {
- new_max = maxx(max_left.top(), left.top());
- }
- max_left.push(new_max);
- }
- void pop_first() {
- if (!right.empty()) {
- right.pop();
- max_right.pop();
- } else {
- migrate();
- if (!right.empty()) {
- right.pop();
- max_right.pop();
- }
- }
- }
- int get_max() {
- int ans = -1e9 - 1e8;
- if (!max_left.empty()) {
- ans = maxx(ans, max_left.top());
- }
- if (!max_right.empty()) {
- ans = maxx(ans, max_right.top());
- }
- return ans;
- }
- bool is_empty() { return left.empty() && right.empty(); }
- private:
- std::stack<int> left;
- std::stack<int> max_left;
- std::stack<int> right;
- std::stack<int> max_right;
- };
- int main() {
- int len, moves, it = 0;
- std::cin >> len;
- std::vector<int> data(len);
- authentic_queue tape;
- for (auto& element : data) {
- std::cin >> element;
- }
- std::cin >> moves;
- tape.push_new(data[0]);
- for (int iter = 0; iter < moves; ++iter) {
- char command;
- std::cin >> command;
- if (command == 'L') {
- tape.pop_first();
- std::cout << tape.get_max() << " ";
- }
- if (command == 'R') {
- ++it;
- tape.push_new(data[it]);
- std::cout << tape.get_max() << " ";
- }
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement