Advertisement
Guest User

Problem4 solved

a guest
Sep 24th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class QueryStack {
  8. public:
  9.     QueryStack() {}
  10.     bool empty() { return vals.empty(); }
  11.     void push(int x) {
  12.         vals.push(x);
  13.         int next_max = (maxs.empty()) ? x : max(maxs.top(), x);
  14.         maxs.push(next_max);
  15.     }
  16.     void pop() {
  17.         vals.pop();
  18.         maxs.pop();
  19.     }
  20.     int top() {
  21.         return vals.top();
  22.     }
  23.     int stack_max() {
  24.         if (!empty())
  25.             return maxs.top();
  26.         else
  27.             return -(1 << 30);
  28.     }
  29. private:
  30.     stack<int> vals, maxs;
  31. };
  32.  
  33. class QueryQueue {
  34. public:
  35.     QueryQueue() { }
  36.     bool empty() { return head.empty() && tail.empty(); }
  37.     void push(int x) { tail.push(x); }
  38.     void pop() {
  39.         if (!head.empty()) head.pop();
  40.         else {
  41.             while (!tail.empty()) {
  42.                 int x = tail.top();
  43.                 tail.pop();
  44.                 head.push(x);
  45.             }
  46.         }
  47.     }
  48.     int queue_max() {
  49.         return max(head.stack_max(), tail.stack_max());
  50.     }
  51. private:
  52.     QueryStack head, tail;
  53. };
  54.  
  55. void solve(vector<int>& seq) {
  56.     int left = 0, right = 1;
  57.     QueryQueue queue;
  58.     queue.push(seq[left]);
  59.     int n_queries; cin >> n_queries;
  60.     for (int i = 0; i < n_queries; ++i) {
  61.         char type; cin >> type;
  62.         if (type == 'R') {
  63.             queue.push(seq[right++]);
  64.         }
  65.         else {
  66.             queue.pop();
  67.             ++left;
  68.         }
  69.         cout << queue.queue_max() << " ";
  70.     }
  71. }
  72.  
  73. int main() {
  74.     int sz; cin >> sz;
  75.     vector<int> seq(sz);
  76.     for (auto& el : seq) cin >> el;
  77.     solve(seq);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement