Advertisement
Derga

Untitled

Aug 22nd, 2023
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class MinQueue {
  9. private:
  10.     vector<int> data;
  11.     size_t dataBegin = 0;
  12.     vector<int> minData;
  13.     size_t minDataBegin = 0;
  14.  
  15. public:
  16.     void push(int val) {
  17.         data.push_back(val);
  18.         while (minData.size() > minDataBegin && minData.back() > val) {
  19.             minData.pop_back();
  20.         }
  21.         minData.push_back(val);
  22.     }
  23.  
  24.     int min() {
  25.         return minData[minDataBegin];
  26.     }
  27.  
  28.     void pop() {
  29.         if (minData[minDataBegin] == data[dataBegin++]) {
  30.             ++minDataBegin;
  31.         }
  32.     }
  33. };
  34.  
  35. int main() {
  36.     MinQueue q;
  37.     int n;
  38.     cin >> n;
  39.     string type;
  40.     int val;
  41.     while (n--) {
  42.         cin >> type;
  43.         if (type == "push") {
  44.             cin >> val;
  45.             q.push(val);
  46.         } else if (type == "pop") {
  47.             q.pop();
  48.         } else {
  49.             cout << q.min() << '\n';
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement