Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
//{{{By oToToT
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
/* include everything for Kotori~ <3 */

using lld = int64_t;
using llu = uint64_t;
using llf = long double;
using PII = pair<int, int>;
using PIL = pair<int, lld>;
using PLI = pair<lld, int>;
using PLL = pair<lld, lld>;
template <typename T> using maxHeap = priority_queue<T, vector<T>, less<T>>;
template <typename T> using minHeap = priority_queue<T, vector<T>, greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)((x).size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while (x--)
/* make code shorter for Di~a~ */

template <typename Iter> ostream &_out(ostream &s, Iter b, Iter e) {
  s << "[";
  for (auto it = b; it != e; it++)
    s << (it == b ? "" : " ") << *it;
  s << "]";
  return s;
}
template <typename A, typename B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  return s << "(" << p.FF << "," << p.SS << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) {
  return _out(s, ALL(c));
}
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x)                                                                \
  if (debug)                                                                   \
  cerr << __PRETTY_FUNCTION__ << ":" << __LINE__ << " - " << (#x) << "="       \
       << (x) << '\n'
template <typename T> void DEBUG(const T &x) {
  if (debug)
    cerr << x;
}
template <typename T, typename... Args>
void DEBUG(const T &head, const Args &...tail) {
  if (debug) {
    cerr << head;
    DEBUG(tail...);
  }
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 200000 + 5;

inline bool isOK(int, int);
int arr[N];

int main(int argc, char *argv[]) {
  if (argc > 1 and string(argv[1]) == "-D")
    debug = 1;
  if (!debug) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (isOK(mid - 1, n))
      r = mid;
    else
      l = mid;
  }
  cout << r << '\n';
  return 0;
}

inline bool isOK(int mx, int n) {
  map<int, int> mp;
  for (int i = 1; i < n; i++) {
    if (arr[i] > arr[i - 1])
      continue;
    int p = arr[i];
    while (p >= 1 and mp[p] == mx) {
      p--;
    }
    if (p == 0)
      return false;
    mp[p]++;
    auto it = mp.upper_bound(p);
    while (it != mp.end())
      it = mp.erase(it);
  }
  return true;
}

Fixed submission:
//{{{By oToToT
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
/* include everything for Kotori~ <3 */

using lld = int64_t;
using llu = uint64_t;
using llf = long double;
using PII = pair<int, int>;
using PIL = pair<int, lld>;
using PLI = pair<lld, int>;
using PLL = pair<lld, lld>;
template <typename T> using maxHeap = priority_queue<T, vector<T>, less<T>>;
template <typename T> using minHeap = priority_queue<T, vector<T>, greater<T>>;
/* define some types for Ruby! */

#define FF first
#define SS second
#define SZ(x) (int)((x).size())
#define ALL(x) begin(x), end(x)
#define PB push_back
#define WC(x) while (x--)
/* make code shorter for Di~a~ */

template <typename Iter> ostream &_out(ostream &s, Iter b, Iter e) {
  s << "[";
  for (auto it = b; it != e; it++)
    s << (it == b ? "" : " ") << *it;
  s << "]";
  return s;
}
template <typename A, typename B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
  return s << "(" << p.FF << "," << p.SS << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) {
  return _out(s, ALL(c));
}
/* make output easier for Ainyan~n~ */

bool debug = 0;
#define DUMP(x)                                                                \
  if (debug)                                                                   \
  cerr << __PRETTY_FUNCTION__ << ":" << __LINE__ << " - " << (#x) << "="       \
       << (x) << '\n'
template <typename T> void DEBUG(const T &x) {
  if (debug)
    cerr << x;
}
template <typename T, typename... Args>
void DEBUG(const T &head, const Args &...tail) {
  if (debug) {
    cerr << head;
    DEBUG(tail...);
  }
}
/* Let's debug with Nico~Nico~Ni */
//}}}
const int N = 200000 + 5;

inline bool isOK(int, int);
int arr[N];

int main(int argc, char *argv[]) {
  if (argc > 1 and string(argv[1]) == "-D")
    debug = 1;
  if (!debug) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (isOK(mid - 1, n))
      r = mid;
    else
      l = mid;
  }
  cout << r << '\n';
  return 0;
}

inline bool isOK(int mx, int n) {
  map<int, int> mp;
  for (int i = 1; i < n; i++) {
    if (arr[i] > arr[i - 1])
      continue;
    if (mx == 0)
      return false;
    int p = arr[i];
    while (p >= 1 and mp[p] == mx) {
      p--;
    }
    if (p == 0)
      return false;
    mp[p]++;
    auto it = mp.upper_bound(p);
    while (it != mp.end())
      it = mp.erase(it);
  }
  return true;
}