description: Mr. Chanek gives you a sequence a indexed from 1 to n. Define f(a) as the number of indices where a_i = i. 
  
  You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the 3-rd element from the sequence [4, 2, 3, 1], the resulting sequence will be [4, 2, 1]. 
  
  You want to remove some elements from a in order to maximize f(a), using zero or more operations. Find the largest possible f(a).
  
  Input
  
  The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the initial length of the sequence.
  
  The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 2 ⋅ 10^5) — the initial sequence a.
  
  Output
  
  Output an integer denoting the largest f(a) that can be obtained by doing zero or more operations.
  
  Examples
  
  Input
  
  
  7
  2 1 4 2 5 3 7
  
  
  Output
  
  
  3
  
  
  Input
  
  
  4
  4 2 3 1
  
  
  Output
  
  
  2
  
  Note
  
  In the first example, f(A) = 3 by doing the following operations.
  
  [2,1,4,2,5,3,7] → [2,1,2,5,3,7] → [1,2,5,3,7] → [1,2,5,3] → [1,2,3]
  
  In the second example, f(A) = 2 and no additional operation is needed.
solution: #include <bits/stdc++.h>
  using namespace std;
  const int N = 2e5 + 5;
  int n, a[N], dp[N], ww, ww1;
  struct nod {
    int val;
    int idx;
    int x;
  };
  nod b[4 * N];
  multiset<pair<int, int> >::iterator it;
  multiset<pair<int, int> > ms[4 * N];
  void upd(int node, pair<int, int> vall) {
    if (!ms[node].size()) {
      ms[node].insert(vall);
      return;
    }
    it = ms[node].lower_bound({vall.first, 0});
    if (it != ms[node].end() && (*it).second >= vall.second) {
      return;
    }
    vector<pair<int, int> > vv;
    vv.clear();
    if (it != ms[node].begin()) {
      it--;
      while (true) {
        if ((*it).second <= vall.second)
          vv.push_back({(*it).first, (*it).second});
        else
          break;
        if (it == ms[node].begin()) break;
        it--;
      }
    }
    it = ms[node].lower_bound({vall.first, 0});
    while (true) {
      if (it == ms[node].end() || (*it).first > vall.first) break;
      if ((*it).second <= vall.second)
        vv.push_back({(*it).first, (*it).second});
      else
        break;
      it++;
    }
    for (int i = 0; i < vv.size(); i++) {
      pair<int, int> jj = {vv[i].first, vv[i].second};
      ms[node].erase(ms[node].find(jj));
    }
    ms[node].insert(vall);
  }
  void update(int node, int le, int ri, int idx, pair<int, int> vall) {
    if (le > idx || ri < idx) {
      return;
    }
    if (le == ri) {
      upd(node, vall);
      return;
    }
    int mid = (le + ri) / 2;
    if (idx <= mid)
      update(2 * node, le, mid, idx, vall);
    else
      update(2 * node + 1, mid + 1, ri, idx, vall);
    upd(node, vall);
  }
  int go(int node, int vall) {
    it = (ms[node].lower_bound({vall, 0}));
    if (it == ms[node].end())
      return 0;
    else
      return (*it).second;
  }
  int getans(int node, int le, int ri, int start, int end, int vall) {
    if (start > ri || end < le) return 0;
    if (le >= start && ri <= end) {
      return go(node, vall);
    }
    int mid = (le + ri) / 2;
    return max(getans(2 * node, le, mid, start, end, vall),
               getans(2 * node + 1, mid + 1, ri, start, end, vall));
  }
  int main() {
    std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      b[i].val = a[i];
      b[i].idx = i;
      b[i].x = a[i] - i;
    }
    int mx = 200000;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      ww1 = i;
      dp[i] = getans(1, 1, mx, 1, b[i].val - 1, b[i].x) + 1;
      if (dp[i] == 1 && b[i].x > 0) dp[i] = 0;
      update(1, 1, mx, b[i].val, {b[i].x, dp[i]});
      ans = max(ans, dp[i]);
    }
    cout << ans;
  }
  