Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 666666;
int n, a[N]; //,b[N];
void ls() {
  static int v[N];
  rep(i, 1, n) v[i] = a[i];
  sort(v + 1, v + n + 1);
  rep(i, 1, n) a[i] = lower_bound(v + 1, v + n + 1, a[i]) - v;
}
int main() {
  read(n);
  rep(i, 1, n) read(a[i]); // ls();
  //	rep(i,1,n)printf("qaq %d\n",a[i]);
  int l = 1, r = n, res = n;
  while (l <= r) {
    int mid = (l + r) >> 1; // memset(b,0,sizeof(b));
    bool gg = 0;
    priority_queue<int> heap;
    map<int, int> b;
    rep(i, 1, n) {
      /*if(a[i-1]<a[i]){
              assert(heap.empty()||heap.top()<=a[i-1]);
      //
      while(!heap.empty()&&heap.top()>a[i-1])b[heap.top()]=0,heap.pop(); }else*/
      if (a[i - 1] >= a[i]) {
        if (mid == 1) {
          gg = 1;
          break;
        }
        int j;
        for (j = a[i]; j >= 1; j--)
          if (b[j] < mid - 1)
            break;
        if (!j) {
          gg = 1;
          break;
        }
        //	printf("find %d <%d>\n",j,a[i]);
        b[j]++;
        heap.push(j); // erase(j+1..a[i])
        while (!heap.empty() && heap.top() > j)
          b[heap.top()] = 0, heap.pop();
      }
      //	rep(j,1,(heap.empty()?0:heap.top()))printf("%d ",b[j]);puts("");
    }
    if (!gg)
      res = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 666666;
int n, a[N]; //,b[N];
void ls() {
  static int v[N];
  rep(i, 1, n) v[i] = a[i];
  sort(v + 1, v + n + 1);
  rep(i, 1, n) a[i] = lower_bound(v + 1, v + n + 1, a[i]) - v;
}
int main() {
  read(n);
  rep(i, 1, n) read(a[i]); // ls();
  //	rep(i,1,n)printf("qaq %d\n",a[i]);
  int l = 1, r = n, res = n;
  while (l <= r) {
    int mid = (l + r) >> 1; // memset(b,0,sizeof(b));
    bool gg = 0;
    priority_queue<int> heap;
    map<int, int> b;
    rep(i, 1, n) {
      /*if(a[i-1]<a[i]){
              assert(heap.empty()||heap.top()<=a[i-1]);
      //
      while(!heap.empty()&&heap.top()>a[i-1])b[heap.top()]=0,heap.pop(); }else*/
      if (a[i - 1] >= a[i]) {
        if (mid == 1) {
          gg = 1;
          break;
        }
        int j;
        for (j = a[i]; j >= 1; j--)
          if (b[j] < mid - 1)
            break;
        if (!j) {
          gg = 1;
          break;
        }
        //	printf("find %d <%d>\n",j,a[i]);
        b[j]++;
        rep(k, j + 1, a[i]) b.erase(k);
        heap.push(j); // erase(j+1..a[i])
        while (!heap.empty() && heap.top() > j)
          b[heap.top()] = 0, heap.pop();
      }
      //	rep(j,1,(heap.empty()?0:heap.top()))printf("%d ",b[j]);puts("");
    }
    if (!gg)
      res = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << res;
  return 0;
}