Mahmoud_Hawara

Untitled

Jul 19th, 2022
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  4. const long long MOD = 1e9 + 7, OO = 1e18;
  5. const double PI = acos(-1);
  6. const int N = 1e6 + 5;
  7. const int dx[4] = {0, 0, 1, -1};
  8. const int dy[4] = {1, -1, 0, 0};
  9.  
  10. long long n, x;
  11.  
  12. int main()
  13. {
  14.     IO
  15.     while (cin >> n && n)
  16.     {
  17.         queue<long long>q, ans;
  18.         stack<long long>st;
  19.         for (int i = 1; i <= n; i++)
  20.         {
  21.             cin >> x;
  22.             q.push(x);
  23.         }
  24.         x = 1;
  25.         while (q.size())
  26.         {
  27.             if (q.front() == x)
  28.             {
  29.                 ans.push(x);
  30.                 q.pop();
  31.                 x++;
  32.             }
  33.             else if (st.size() && st.top() == x)
  34.             {
  35.                 ans.push(x);
  36.                 st.pop();
  37.                 x++;
  38.             }
  39.             else
  40.             {
  41.                 st.push(q.front());
  42.                 q.pop();
  43.             }
  44.         }
  45.         while (st.size())
  46.         {
  47.             ans.push(st.top());
  48.             st.pop();
  49.         }
  50.         x = 1;
  51.         bool ok = 1;
  52.         while (ans.size() && ok)
  53.         {
  54.             if (ans.front() == x)
  55.             {
  56.                 ans.pop();
  57.                 x++;
  58.             }
  59.             else ok = 0;
  60.         }
  61.         ok ? cout << "yes\n" : cout << "no\n";
  62.     }
  63.     return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment