Problem p03202 (C++)

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

int n, m;
int i, j, k;
string str;
int a[200000];

bool test() {
  vector<int> d, dv;
  d.push_back(a[0]);
  dv.push_back(1);

  for (i = 1; i < n; i++) {
    if (a[i] > a[i - 1]) {
      d.push_back(a[i]);
      dv.push_back(1);
    } else if (a[i] == a[i - 1]) {
      dv[dv.size() - 1]++;
    } else {
      while (d[d.size() - 1] > a[i]) {
        j = d[d.size() - 1] - 1;
        k = (dv[dv.size() - 1] - 1) / m;
        d.pop_back();
        dv.pop_back();
        if (k > 0) {
          if (j == 0)
            return false;
          else if (d.size() > 0 && d[d.size() - 1] == j) {
            dv[dv.size() - 1] += k;
          } else {
            d.push_back(j);
            dv.push_back(k + 1);
          }
        } else if (d.size() == 0) {
          break;
        }
      }
      if (d.size() > 0 && d[d.size() - 1] == a[i]) {
        dv[dv.size() - 1]++;
      } else {
        d.push_back(a[i]);
        dv.push_back(2);
      }
    }
  }
  while (d.size() > 0) {
    j = d[d.size() - 1] - 1;
    k = (dv[dv.size() - 1] - 1) / m;
    d.pop_back();
    dv.pop_back();
    if (k > 0) {
      if (j == 0)
        return false;
      else if (d.size() > 0 && d[d.size() - 1] == j) {
        dv[dv.size() - 1] += k;
      } else {
        d.push_back(j);
        dv.push_back(k + 1);
      }
    }
  }
  return true;
}
int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  int s = 0, e = n;
  while (e - s > 1) {
    m = (e + s + 1) / 2;
    if (test()) {
      e = m;
    } else {
      s = m;
    }
  }
  cout << e << endl;
}

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

int n, m;
int i, j, k;
string str;
int a[200000];

bool test() {
  if (m == 1) {
    for (i = 1; i < n; i++) {
      if (a[i] <= a[i - 1]) {
        return false;
      }
    }
  }
  vector<int> d, dv;
  d.push_back(a[0]);
  dv.push_back(1);

  for (i = 1; i < n; i++) {
    if (a[i] > a[i - 1]) {
      d.push_back(a[i]);
      dv.push_back(1);
    } else if (a[i] == a[i - 1]) {
      dv[dv.size() - 1]++;
    } else {
      while (d[d.size() - 1] > a[i]) {
        j = d[d.size() - 1] - 1;
        k = (dv[dv.size() - 1] - 1) / m;
        d.pop_back();
        dv.pop_back();
        if (k > 0) {
          if (j == 0)
            return false;
          else if (d.size() > 0 && d[d.size() - 1] == j) {
            dv[dv.size() - 1] += k;
          } else {
            d.push_back(j);
            dv.push_back(k + 1);
          }
        } else if (d.size() == 0) {
          break;
        }
      }
      if (d.size() > 0 && d[d.size() - 1] == a[i]) {
        dv[dv.size() - 1]++;
      } else {
        d.push_back(a[i]);
        dv.push_back(2);
      }
    }
  }
  while (d.size() > 0) {
    j = d[d.size() - 1] - 1;
    k = (dv[dv.size() - 1] - 1) / m;
    d.pop_back();
    dv.pop_back();
    if (k > 0) {
      if (j == 0)
        return false;
      else if (d.size() > 0 && d[d.size() - 1] == j) {
        dv[dv.size() - 1] += k;
      } else {
        d.push_back(j);
        dv.push_back(k + 1);
      }
    }
  }
  return true;
}
int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  int s = 0, e = n;
  while (e - s > 1) {
    m = (e + s + 1) / 2;
    if (test()) {
      e = m;
    } else {
      s = m;
    }
  }
  cout << e << endl;
}