Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- const long long MOD = 1e9 + 7, OO = 1e18;
- const double PI = acos(-1);
- const int N = 1e6 + 5;
- const int dx[4] = {0, 0, 1, -1};
- const int dy[4] = {1, -1, 0, 0};
- long long n, x;
- int main()
- {
- IO
- while (cin >> n && n)
- {
- queue<long long>q, ans;
- stack<long long>st;
- for (int i = 1; i <= n; i++)
- {
- cin >> x;
- q.push(x);
- }
- x = 1;
- while (q.size())
- {
- if (q.front() == x)
- {
- ans.push(x);
- q.pop();
- x++;
- }
- else if (st.size() && st.top() == x)
- {
- ans.push(x);
- st.pop();
- x++;
- }
- else
- {
- st.push(q.front());
- q.pop();
- }
- }
- while (st.size())
- {
- ans.push(st.top());
- st.pop();
- }
- x = 1;
- bool ok = 1;
- while (ans.size() && ok)
- {
- if (ans.front() == x)
- {
- ans.pop();
- x++;
- }
- else ok = 0;
- }
- ok ? cout << "yes\n" : cout << "no\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment