Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- stack<pair<int, int> > s1, s2;
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- int inp;
- cin >> inp;
- if (inp == 0) {
- if (s1.empty() && s2.empty()) {
- cout << "-1\n";
- continue;
- }
- if (s1.empty() || s2.empty())
- if (s1.empty())
- cout << s2.top().second << '\n';
- else
- cout << s1.top().second << '\n';
- else
- cout << min(s1.top().second, s2.top().second) << '\n';
- if (s2.empty())
- while (!s1.empty()) {
- int curr = s1.top().first;
- s1.pop();
- int min_a;
- if (s2.empty())
- min_a = curr;
- else
- min_a = min(curr, s2.top().second);
- s2.push(make_pair(curr, min_a));
- }
- s2.pop();
- } else {
- int min_a;
- if (s1.empty())
- min_a = inp;
- else
- min_a = min(inp, s1.top().second);
- s1.push(make_pair(inp, min_a));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement