Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i++)
#define re0(i, n) for (rint i = 0; i < (int)n; i++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;

typedef long long lo;

template <typename tp> inline void read(tp &x) {
  x = 0;
  char c = getchar();
  int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
    ;
  if (f)
    x = -x;
}
const int N = 3e5 + 233;
int top = 0, st[N], val[N];
int n, a[N];

inline void init(void) {
  top = 0;
  memset(st, 0, sizeof st);
  memset(val, 0, sizeof val);
}

inline bool add(int x, int mid, int fff = 0) {
  if (!x)
    return false;
  // cout << x << " " << mid << "ss\n";
  if (st[top] != x) {
    st[++top] = x;
    val[top] = fff;
  }
  if (top && st[top] == x) {
    if (val[top] == mid) {
      --top;
      if (!add(x - 1, mid, 1))
        return false;
      st[++top] = x;
      val[top] = 1;
    } else {
      ++val[top];
    }
  }
  return true;
}

inline bool check(int mid) {
  init();
  rep(i, n) {
    if (!top || st[top] <= a[i]) {
      if (!add(a[i], mid))
        return false;
    } else {
      while (top && st[top] > a[i])
        --top;
      if (st[top] != a[i]) {
        st[++top] = a[i];
        val[top] = 1;
      } // else {
      //   if (val[top] == mid) {
      //     -- top;
      //     if (!add(a[i] - 1, mid))
      //       return false;
      //     st[++ top] = a[i];
      //     val[top] = 0;
      //   } else {
      //     ++ val[top];
      //   }
      // }
      // rep (i, top) cout << st[i] << " "; cout << "\n";
      // rep (i, top) cout << val[i] << " "; cout << "\n";
      if (!add(a[i], mid))
        return false;
    }
  }
  return true;
}

int main(void) {
  read(n);
  rep(i, n) read(a[i]);
  int l = 1, r = 1e9;
  while (l < r) {
    int mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << r << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (n); i++)
#define re0(i, n) for (rint i = 0; i < (int)n; i++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;

typedef long long lo;

template <typename tp> inline void read(tp &x) {
  x = 0;
  char c = getchar();
  int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
    ;
  if (f)
    x = -x;
}
const int N = 3e5 + 233;
int top = 0, st[N], val[N];
int n, a[N];

inline void init(void) {
  top = 0;
  memset(st, 0, sizeof st);
  memset(val, 0, sizeof val);
}

inline bool add(int x, int mid, int fff = 0) {
  if (!x)
    return false;
  // cout << x << " " << mid << "ss\n";
  if (st[top] != x) {
    st[++top] = x;
    val[top] = fff;
  }
  if (top && st[top] == x) {
    if (val[top] == mid) {
      --top;
      if (!add(x - 1, mid, 1))
        return false;
      st[++top] = x;
      val[top] = 1;
    } else {
      ++val[top];
    }
  }
  return true;
}

inline bool check(int mid) {
  init();
  rep(i, n) {
    if (!top || st[top] <= a[i]) {
      if (!add(a[i], mid))
        return false;
    } else {
      while (top && st[top] > a[i])
        --top;
      if (st[top] != a[i]) {
        st[++top] = a[i];
        val[top] = 1;
      } // else {
      //   if (val[top] == mid) {
      //     -- top;
      //     if (!add(a[i] - 1, mid))
      //       return false;
      //     st[++ top] = a[i];
      //     val[top] = 0;
      //   } else {
      //     ++ val[top];
      //   }
      // }
      // rep (i, top) cout << st[i] << " "; cout << "\n";
      // rep (i, top) cout << val[i] << " "; cout << "\n";
      if (!add(a[i], mid))
        return false;
    }
  }
  return true;
}

int main(void) {
  read(n);
  rep(i, n) read(a[i]);
  int f = 1;
  rep(i, n - 1) f &= a[i] < a[i + 1];
  if (f) {
    cout << "1\n";
    return 0;
  }
  int l = 2, r = 1e9;
  while (l < r) {
    int mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << r << "\n";
}