Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MaxN = 200000 + 5;

int N;
int A[MaxN];
vector<pii> vec;

void init() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]);
}

bool is_increase() {
  for (int i = 2; i <= N; ++i)
    if (A[i] <= A[i - 1])
      return false;
  return true;
}

inline bool check(int m) {
  vec.clear();

  vec.push_back(make_pair(1, 0));
  int id = A[1];
  if (id != 1)
    vec.push_back(make_pair(id, 0));
  for (int i = 2; i <= N; ++i) {
    /* printf("m = %d, i = %d\n", m, i);
    for (auto v : vec) {
        printf("[%d, %d]\n", v.first, v.second);
    } */
    if (A[i] > id) {
      id = A[i];
      vec.push_back(make_pair(id, 0));
    } else if (A[i] == id) {
      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));

      int worst = id;
      while ((vec.end() - 1)->second == m - 1) {
        worst = (vec.end() - 1)->first - 1;
        vec.erase(vec.end() - 1);
        if (vec.empty() == true)
          return false;
        if ((vec.end() - 1)->first != worst)
          vec.push_back(make_pair(worst, 0));
      }

      pii p = *(vec.end() - 1);
      p.second++;
      vec.erase(vec.end() - 1);
      vec.push_back(p);

      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));
    } else {
      id = A[i];
      while ((vec.end() - 1)->first > id) {
        vec.erase(vec.end() - 1);
        if (vec.empty() == true)
          return false;
      }

      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));

      int worst = id;
      while ((vec.end() - 1)->second == m - 1) {
        worst = (vec.end() - 1)->first - 1;
        vec.erase(vec.end() - 1);
        if (vec.empty() == true)
          return false;
        if ((vec.end() - 1)->first != worst)
          vec.push_back(make_pair(worst, 0));
      }

      pii p = *(vec.end() - 1);
      p.second++;
      vec.erase(vec.end() - 1);
      vec.push_back(p);

      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));
    }

    // printf("m = %d, id = %d\n", m, id);
  }

  return true;
}

void solve() {
  if (is_increase() == true) {
    cout << 1 << endl;
    return;
  }

  int lbound = 1, rbound = N, ans;
  while (lbound <= rbound) {
    int mid = (lbound + rbound) >> 1;
    if (check(mid) == true) {
      ans = mid;
      rbound = mid - 1;
    } else {
      lbound = mid + 1;
    }
  }
  cout << ans << endl;
}

int main() {
  init();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MaxN = 200000 + 5;

int N;
int A[MaxN];
vector<pii> vec;

void init() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]);
}

bool is_increase() {
  for (int i = 2; i <= N; ++i)
    if (A[i] <= A[i - 1])
      return false;
  return true;
}

inline bool check(int m) {
  vec.clear();

  vec.push_back(make_pair(1, 0));
  int id = A[1];
  if (id != 1)
    vec.push_back(make_pair(id, 0));
  for (int i = 2; i <= N; ++i) {
    /* printf("m = %d, i = %d\n", m, i);
    for (auto v : vec) {
        printf("[%d, %d]\n", v.first, v.second);
    } */
    if (A[i] > id) {
      id = A[i];
      vec.push_back(make_pair(id, 0));
    } else if (A[i] == id) {
      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));

      int worst = id;
      while ((vec.end() - 1)->second == m - 1) {
        worst = (vec.end() - 1)->first - 1;
        vec.erase(vec.end() - 1);
        if (vec.empty() == true)
          return false;
        if ((vec.end() - 1)->first != worst)
          vec.push_back(make_pair(worst, 0));
      }

      pii p = *(vec.end() - 1);
      p.second++;
      vec.erase(vec.end() - 1);
      vec.push_back(p);

      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));
    } else {
      id = A[i];
      while ((vec.end() - 1)->first > id) {
        vec.erase(vec.end() - 1);
        if (vec.empty() == true)
          return false;
      }

      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));

      int worst = id;
      while ((vec.end() - 1)->second == m - 1) {
        worst = (vec.end() - 1)->first - 1;
        vec.erase(vec.end() - 1);
        if (vec.empty() == true)
          return false;
        if ((vec.end() - 1)->first != worst)
          vec.push_back(make_pair(worst, 0));
      }

      pii p = *(vec.end() - 1);
      p.second++;
      vec.erase(vec.end() - 1);
      vec.push_back(p);

      if ((vec.end() - 1)->first != id)
        vec.push_back(make_pair(id, 0));
    }

    // printf("m = %d, id = %d\n", m, id);
  }

  return true;
}

void solve() {
  if (is_increase() == true) {
    cout << 1 << endl;
    return;
  }

  int lbound = 2, rbound = N, ans;
  while (lbound <= rbound) {
    int mid = (lbound + rbound) >> 1;
    if (check(mid) == true) {
      ans = mid;
      rbound = mid - 1;
    } else {
      lbound = mid + 1;
    }
  }
  cout << ans << endl;
}

int main() {
  init();
  solve();
  return 0;
}