Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <unordered_set>
// #include <unordered_map>
// #include <bits/stdc++.h>

#define MP(A, B) make_pair(A, B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define fi first
#define se second

using namespace std;

template <typename T> inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod eps = 1e-11;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 998244353;
const int MAXN = 600005;
const int INF = 0x3f3f3f3f; // 1061109567
/*--------------------------------------------------------------------*/
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
map<int, int> Map;
int a[MAXN], n;
bool check(int k) {
  Map.clear();
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (!Map.empty()) {
      int t = Map.rbegin()->fi;
      if (t > a[i])
        Map.erase(t);
      else
        break;
    }
    int j = a[i];
    while (j && Map[j] == k - 1)
      Map.erase(j), j--;
    if (j == 0)
      return 0;
    Map[j]++;
  }
  return 1;
}
int main() {
  n = read();
  int l = 1, r = n, flag = 1;
  for (int i = 1; i <= n; i++)
    a[i] = read(), flag &= (a[i] > a[i - 1]);
  if (flag) {
    puts("1");
    return 0;
  }
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <unordered_set>
// #include <unordered_map>
// #include <bits/stdc++.h>

#define MP(A, B) make_pair(A, B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define fi first
#define se second

using namespace std;

template <typename T> inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod eps = 1e-11;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 998244353;
const int MAXN = 600005;
const int INF = 0x3f3f3f3f; // 1061109567
/*--------------------------------------------------------------------*/
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
map<int, int> Map;
int a[MAXN], n;
bool check(int k) {
  Map.clear();
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (!Map.empty()) {
      int t = Map.rbegin()->fi;
      if (t > a[i])
        Map.erase(t);
      else
        break;
    }
    int j = a[i];
    while (j && Map[j] == k - 1)
      Map.erase(j), j--;
    if (j == 0)
      return 0;
    Map[j]++;
  }
  return 1;
}
int main() {
  n = read();
  int l = 2, r = n, flag = 1;
  for (int i = 1; i <= n; i++)
    a[i] = read(), flag &= (a[i] > a[i - 1]);
  if (flag) {
    puts("1");
    return 0;
  }
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}