Advertisement
Dimaush

P

Oct 15th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(nullptr);
  9.  
  10.     unsigned n, p, c;
  11.     vector<unsigned> b;
  12.     vector<string> answer;
  13.  
  14.     cin >> n;
  15.     p = 1;
  16.     for (unsigned i = 0; i < n; ++i) {
  17.         cin >> c;
  18.         answer.push_back("push");
  19.         if (c == p) {
  20.             answer.push_back("pop");
  21.             ++p;
  22.         } else {
  23.             b.push_back(c);
  24.         }
  25.         while (b.size() > 0 && *b.rbegin() == p) {
  26.             b.pop_back();
  27.             answer.push_back("pop");
  28.             ++p;
  29.         }
  30.     }
  31.  
  32.     if (p != n + 1) {
  33.         cout << "impossible" << endl;
  34.     } else {
  35.         for (string action: answer) {
  36.             cout << action << endl;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement