Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \
  sort(all(v));                                                                \
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
  type x;                                                                      \
  cin >> x
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PRINT(f)                                                               \
  if ((f)) {                                                                   \
    cout << (TRUE__) << endl;                                                  \
  } else {                                                                     \
    cout << FALSE__ << endl;                                                   \
  }
#define RS(N) resize(N)
#define CINV(v, N)                                                             \
  do {                                                                         \
    v.RS(N);                                                                   \
    rep(i, N) cin >> v[i];                                                     \
  } while (0);
#define RCINV(v, N)                                                            \
  do {                                                                         \
    v.RS(N);                                                                   \
    rrep(i, N) cin >> v[i];                                                    \
  } while (0);

#define MOD 1000000007

void init();
void solve();

signed main() {
  init();
  solve();
}

int N;
vector<int> A;

void init() {
  cin >> N;
  CINV(A, N);
}

int X;

inline void add(int x, map<int, int> &m) {
  m[x]++;
  if (m[x] == X) {
    m.erase(x);
    add(x - 1, m);
  }
}

inline bool isok(int x) {
  X = x;
  int pre = 0;
  map<int, int> m;
  rep(i, N) {
    if (pre < A[i]) {
      pre = A[i];
      continue;
    }
    pre = A[i];
    add(A[i], m);
    while (1) {
      auto itr = --m.end();
      if (x > A[i])
        m.erase(itr);
      else
        break;
    }
    if (m.count(0))
      return false;
  }
  return true;
}

void solve() {
  bool f = true;
  rep(i, N - 1) { f &= A[i] < A[i + 1]; }
  if (f) {
    cout << 1 << endl;
    return;
  }
  int ok = 1000000000;
  int ng = 1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isok(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v)                                                              \
  sort(all(v));                                                                \
  (v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
  type x;                                                                      \
  cin >> x
#define TRUE__ "Yes"
#define FALSE__ "No"
#define PRINT(f)                                                               \
  if ((f)) {                                                                   \
    cout << (TRUE__) << endl;                                                  \
  } else {                                                                     \
    cout << FALSE__ << endl;                                                   \
  }
#define RS(N) resize(N)
#define CINV(v, N)                                                             \
  do {                                                                         \
    v.RS(N);                                                                   \
    rep(i, N) cin >> v[i];                                                     \
  } while (0);
#define RCINV(v, N)                                                            \
  do {                                                                         \
    v.RS(N);                                                                   \
    rrep(i, N) cin >> v[i];                                                    \
  } while (0);

#define MOD 1000000007

void init();
void solve();

signed main() {
  init();
  solve();
}

int N;
vector<int> A;

void init() {
  cin >> N;
  CINV(A, N);
}

int X;

inline void add(int x, map<int, int> &m) {
  m[x]++;
  if (m[x] == X) {
    m.erase(x);
    add(x - 1, m);
  }
}

inline bool isok(int x) {
  X = x;
  int pre = 0;
  map<int, int> m;
  rep(i, N) {
    if (pre < A[i]) {
      pre = A[i];
      continue;
    }
    pre = A[i];
    add(A[i], m);
    while (1) {
      auto itr = --m.end();
      int x = (*itr).first;
      if (x > A[i])
        m.erase(itr);
      else
        break;
    }
    if (m.count(0))
      return false;
  }
  return true;
}

void solve() {
  bool f = true;
  rep(i, N - 1) { f &= A[i] < A[i + 1]; }
  if (f) {
    cout << 1 << endl;
    return;
  }
  int ok = 1000000000;
  int ng = 1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isok(mid))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
}