Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "1"                                                                \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
#define PI (acos(-1))
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------

//--MAIN-----------------------------------
void Main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  bool okk = false;
  rep(i, N - 1) if (A[i] >= A[i + 1]) okk = true;
  if (!okk)
    dame;

  /*bis*/ ll low = 0, high = 201010; // min-1, max+1
  while (abs(high - low) > 1) {
    ll mid = (high + low) / 2;
    bool ok = true;
    deque<P> dq; // 位置,文字番号
    reps(i, 1, N) {
      if (A[i] > A[i - 1])
        continue;
      while (!dq.empty() and dq.back().first > A[i]) {
        dq.pop_back();
      }
      if (!dq.empty() and dq.back().first == A[i]) {
        dq.back().second++;
      } else {
        dq.emplace_back(A[i], 1);
      }
      while (dq.back().second == mid) {
        ll d = dq.back().first;
        if (d == 1) {
          ok = false;
          i = N + 1;
          break;
        }
        dq.pop_back();
        if (!dq.empty() and dq.back().first == d - 1) {
          dq.back().second++;
        } else {
          dq.emplace_back(d - 1, 1);
        }
      }
    }

    (ok ? high : low) = mid;
  } // if ok, okdir = mid
  cout << high << "\n";
}
//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------


Fixed submission:
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "1"                                                                \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
#define PI (acos(-1))
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------

//--MAIN-----------------------------------
void Main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  bool okk = false;
  rep(i, N - 1) if (A[i] >= A[i + 1]) okk = true;
  if (!okk)
    dame;

  /*bis*/ ll low = 1, high = 201010; // min-1, max+1
  while (abs(high - low) > 1) {
    ll mid = (high + low) / 2;
    bool ok = true;
    deque<P> dq; // 位置,文字番号
    reps(i, 1, N) {
      if (A[i] > A[i - 1])
        continue;
      while (!dq.empty() and dq.back().first > A[i]) {
        dq.pop_back();
      }
      if (!dq.empty() and dq.back().first == A[i]) {
        dq.back().second++;
      } else {
        dq.emplace_back(A[i], 1);
      }
      while (dq.back().second == mid) {
        ll d = dq.back().first;
        if (d == 1) {
          ok = false;
          i = N + 1;
          break;
        }
        dq.pop_back();
        if (!dq.empty() and dq.back().first == d - 1) {
          dq.back().second++;
        } else {
          dq.emplace_back(d - 1, 1);
        }
      }
    }

    (ok ? high : low) = mid;
  } // if ok, okdir = mid
  cout << high << "\n";
}
//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------