Problem p03202 (C++)

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int n;
  cin >> n;
  VI a(n);
  REP(i, n) { a[i] = in(); }

  int ok = n, ng = 0;
  while (ok - ng > 1) {
    int k = (ok + ng) / 2;
    bool f = true;
    map<int, int> mp;

    // cout << "   " << k << endl;
    FOR(i, 1, n - 1) {
      // cout << i << endl;
      if (a[i] > a[i - 1])
        continue;
      auto itr = mp.lower_bound(a[i]);
      auto itr2 = mp.lower_bound(a[i - 1]);
      mp.erase(itr, itr2);
      int x = a[i] - 1;
      mp[x]++;
      while (x > 0) {
        if (mp[x] == k) {
          mp.erase(x);
          mp[x - 1]++;
          x--;
        } else {
          break;
        }
      }
      if (mp[0] >= k) {
        f = false;
        break;
      }

      // for (P p : mp) cout << p.first << " " << p.second << endl;
    }

    if (f)
      ok = k;
    else
      ng = k;
  }
  cout << ok << endl;
  return 0;
}


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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

int main() {
  int n;
  cin >> n;
  VI a(n);
  REP(i, n) { a[i] = in(); }

  bool one = true;
  REP(i, n - 1) {
    if (a[i] >= a[i + 1])
      one = false;
  }
  if (one) {
    cout << 1 << endl;
    return 0;
  }

  int ok = n, ng = 1;
  while (ok - ng > 1) {
    int k = (ok + ng) / 2;
    bool f = true;
    map<int, int> mp;

    // cout << "   " << k << endl;
    FOR(i, 1, n - 1) {
      // cout << i << endl;
      if (a[i] > a[i - 1])
        continue;
      auto itr = mp.lower_bound(a[i]);
      auto itr2 = mp.lower_bound(a[i - 1]);
      mp.erase(itr, itr2);
      int x = a[i] - 1;
      mp[x]++;
      while (x > 0) {
        if (mp[x] == k) {
          mp.erase(x);
          mp[x - 1]++;
          x--;
        } else {
          break;
        }
      }
      if (mp[0] >= k) {
        f = false;
        break;
      }

      // for (P p : mp) cout << p.first << " " << p.second << endl;
    }

    if (f)
      ok = k;
    else
      ng = k;
  }
  cout << ok << endl;
  return 0;
}