Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

bool ispossible(vector<int> &A, ll d) {
  int N = A.size();
  map<ll, ll> digit;
  for (int i = 1; i < N; i++) {
    if (A[i] <= A[i - 1]) {
      bool cont = true;
      int now = A[i];
      while (cont && now != 0) {
        cont = false;
        digit[now]++;
        if (digit[now] == d) {
          digit.erase(now);
          now--;
          cont = true;
        }
      }
      if (now == 0)
        return false;
      for (auto it = digit.lower_bound(A[i] + 1); it != digit.end();
           it = digit.erase(it))
        ;
    }
  }
  return true;
}

void solve() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll ok = 200000;
  ll ng = 0;

  while (ok - ng > 1) {
    ll c = (ok + ng) / 2;
    if (ispossible(A, c)) {
      ok = c;
    } else {
      ng = c;
    }
  }
  cout << ok << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}


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

using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

bool ispossible(vector<int> &A, ll d) {
  int N = A.size();
  if (d == 1) {
    for (int i = 0; i + 1 < N; i++) {
      if (A[i + 1] <= A[i]) {
        return false;
      }
    }
    return true;
  }
  map<ll, ll> digit;
  for (int i = 1; i < N; i++) {
    if (A[i] <= A[i - 1]) {
      bool cont = true;
      int now = A[i];
      while (cont && now != 0) {
        cont = false;
        digit[now]++;
        if (digit[now] == d) {
          digit.erase(now);
          now--;
          cont = true;
        }
      }
      if (now == 0)
        return false;
      for (auto it = digit.lower_bound(A[i] + 1); it != digit.end();
           it = digit.erase(it))
        ;
    }
  }
  return true;
}

void solve() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  ll ok = 200000;
  ll ng = 0;

  while (ok - ng > 1) {
    ll c = (ok + ng) / 2;
    if (ispossible(A, c)) {
      ok = c;
    } else {
      ng = c;
    }
  }
  cout << ok << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}