Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false)
#define pdl cout << "*" << endl
#define MOD 1000000007
#define INF 1000000000
#define INFLL 1000000000000000000ll
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define long int64_t
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef priority_queue<pii, vector<pii>, greater<pii>> min_pq;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<> dis(0, INF << 1);
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int N = 200001;

int a[N];

bool check(int k, int n) {
  unordered_map<int, int> m;
  stack<int> s;
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (!s.empty() and s.top() > a[i]) {
      m[s.top()] = 0;
      s.pop();
    }
    int p = a[i];
    while (p) {
      m[p]++;
      if (s.empty() or s.top() != p)
        s.push(p);
      if (m[p] == k) {
        m[p] = 0;
        s.pop();
        p--;
      } else
        break;
    }
    if (p == 0)
      return false;
  }
  return true;
}

int main() {
  fio;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 1, r = n;
  while (l != r) {
    int mid = l + r >> 1;
    if (check(mid, n))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false)
#define pdl cout << "*" << endl
#define MOD 1000000007
#define INF 1000000000
#define INFLL 1000000000000000000ll
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define long int64_t
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef priority_queue<pii, vector<pii>, greater<pii>> min_pq;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<> dis(0, INF << 1);
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int N = 200001;

int a[N];

bool check(int k, int n) {
  unordered_map<int, int> m;
  stack<int> s;
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    if (k == 1)
      return false;
    while (!s.empty() and s.top() > a[i]) {
      m[s.top()] = 0;
      s.pop();
    }
    int p = a[i];
    while (p) {
      m[p]++;
      if (s.empty() or s.top() != p)
        s.push(p);
      if (m[p] == k) {
        m[p] = 0;
        s.pop();
        p--;
      } else
        break;
    }
    if (p == 0)
      return false;
  }
  return true;
}

int main() {
  fio;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 1, r = n;
  while (l != r) {
    int mid = l + r >> 1;
    if (check(mid, n))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}