Advertisement
Nik_Perepelov

Очередь с поддержкой минимума и рейтрейсинга

Dec 8th, 2020
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     stack<pair<int, int> > s1, s2;
  7.     int n;
  8.     cin >> n;
  9.     for (int i = 0; i < n; i++) {
  10.         int inp;
  11.         cin >> inp;
  12.         if (inp == 0) {
  13.             if (s1.empty() && s2.empty()) {
  14.                 cout << "-1\n";
  15.                 continue;
  16.             }
  17.  
  18.             if (s1.empty() || s2.empty())
  19.                 if (s1.empty())
  20.                     cout << s2.top().second << '\n';
  21.                 else
  22.                     cout << s1.top().second << '\n';
  23.             else
  24.                 cout << min(s1.top().second, s2.top().second) << '\n';
  25.  
  26.             if (s2.empty())
  27.                 while (!s1.empty()) {
  28.                     int curr = s1.top().first;
  29.                     s1.pop();
  30.                     int min_a;
  31.                     if (s2.empty())
  32.                         min_a = curr;
  33.                     else
  34.                         min_a = min(curr, s2.top().second);
  35.                     s2.push(make_pair(curr, min_a));
  36.                 }
  37.             s2.pop();
  38.         } else {
  39.             int min_a;
  40.             if (s1.empty())
  41.                 min_a = inp;
  42.             else
  43.                 min_a = min(inp, s1.top().second);
  44.             s1.push(make_pair(inp, min_a));
  45.         }
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement