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>
  #pragma warning(disable : 4996)
  #pragma comment(linker, "/STACK:16777216")
  using namespace std;
  const int INF = 1000000000 + 1e8;
  const long long LINF = 2000000000000000000;
  const int N = 3e5 + 1010;
  int t[4 * N];
  int query(int v, int vl, int vr, int l, int r) {
    if (r < vl || vr < l) return -INF;
    if (vl >= l && vr <= r) return t[v];
    int vm = (vl + vr) / 2;
    return max(query(2 * v + 1, vl, vm, l, r),
               query(2 * v + 2, vm + 1, vr, l, r));
  }
  void modify(int v, int vl, int vr, int pos, int val) {
    if (vl == vr) {
      t[v] = max(t[v], val);
      return;
    }
    int vm = (vl + vr) / 2;
    if (pos <= vm) {
      modify(2 * v + 1, vl, vm, pos, val);
    } else
      modify(2 * v + 2, vm + 1, vr, pos, val);
    t[v] = max(t[2 * v + 1], t[2 * v + 2]);
  }
  void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, vector<int>> mp;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      mp[x].push_back(i + 1);
    }
    int ans = 0;
    vector<int> res(n + 1);
    for (auto to : mp) {
      int ai = to.first;
      for (auto i : to.second) {
        if (i - ai >= 0) {
          res[i] = max(1, query(0, 0, N - 1, 0, i - ai) + 1);
          ans = max(ans, res[i]);
        }
      }
      for (auto i : to.second) {
        if (i - ai >= 0) {
          modify(0, 0, N - 1, i - ai, res[i]);
        }
      }
    }
    cout << ans;
  }
  int main() {
    srand(time(0));
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tst = 1;
    while (tst--) {
      solve();
    }
  }
  