#include<bits/stdc++.h>
using namespace std;
bool ok(deque < int > tot, vector < int > cur){
  if(cur.back() <= tot[0])return true; 
  if(cur[0] >= tot.back())return true;
  for(int i = 1; i < tot.size(); i++){
    if(cur[0] >= tot[i - 1] && cur.back() <= tot[i])
      return true;
  }
  return false;
}
int main(){
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  int a[n + 1], idx = -1;
  deque < int > tot;
  for(int i = 1; i <= n; i++)
    cin >> a[i];
  tot.push_back(a[1]);
  for(int i = 2; i <= n; i++){
    if(a[i] < a[i - 1])break;
    tot.push_back(a[i]);
    idx = i;
  }
  int ans = 1;
  vector < int > cur{a[idx + 1]};
  for(int i = idx + 2; i <= n; i++){
    // cout << "tot: ";
    // for(auto j : tot)cout << j << ' ';cout << endl;
    // cout << "cur: ";
    // for(auto j : cur)cout << j << ' ';cout << endl;
    if(a[i] < a[i - 1]){
      multiset < int > st;
      for(auto j : tot)st.insert(j);
      for(auto j : cur)st.insert(j);
      tot.clear();
      cur = {a[i]};
      for(auto j : st)tot.push_back(j);
      ans++;
      continue;
    }
    cur.push_back(a[i]);
    if(ok(tot, cur) == false){
      cur.pop_back();
      multiset < int > st;
      for(auto j : tot)st.insert(j);
      for(auto j : cur)st.insert(j);
      tot.clear();
      cur = {a[i]};
      for(auto j : st)tot.push_back(j);
      ans++;
    }
  }
  cout << ans + 1 << endl;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |