Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int n;
int a[200010];

bool solve(int m) {
  map<int, int> b;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] < a[i])
      continue;
    map<int, int>::iterator itr = b.end();
    if (itr == b.begin())
      goto f;
    itr--;
    while (1) {
      if (itr->fr <= a[i])
        break;
      if (itr == b.begin()) {
        b.erase(itr);
        break;
      }
      b.erase(itr--);
    }
  f:;
    b[a[i]]++;
    int loc = a[i];
    while (b[loc] == m) {
      if (loc == 1)
        return false;
      b[loc] = 0;
      b[loc - 1]++;
      loc--;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }

  int l = 1, r = 200000;
  while (l < r) {
    int m = (l + r) / 2;
    if (solve(m))
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

int n;
int a[200010];

bool solve(int m) {
  map<int, int> b;
  for (int i = 1; i <= n; i++) {
    if (a[i - 1] < a[i])
      continue;
    map<int, int>::iterator itr = b.end();
    if (itr == b.begin())
      goto f;
    itr--;
    while (1) {
      if (itr->fr <= a[i])
        break;
      if (itr == b.begin()) {
        b.erase(itr);
        break;
      }
      b.erase(itr--);
    }
  f:;
    b[a[i]]++;
    int loc = a[i];
    while (b[loc] == m) {
      if (loc == 1)
        return false;
      b[loc] = 0;
      b[loc - 1]++;
      loc--;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }

  bool one = true;
  for (int i = 1; i + 1 <= n; i++) {
    one &= a[i] < a[i + 1];
  }
  if (one) {
    puts("1");
    return 0;
  }

  int l = 2, r = 200000;
  while (l < r) {
    int m = (l + r) / 2;
    if (solve(m))
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}