prithivMLmods/Coder-Stat huggingface dataset:
Problem p03202 (C++)

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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cout << #x << " = " << (x) << endl

const int MN = 100010;

int N;
int A[MN];

bool ok(int x) {
  set<pii> S;

  int cl = A[0];

  map<int, int> M;
  for (int i = 1; i < N; ++i) {
    if (A[i] <= cl) {
      while (!M.empty()) {
        auto it = M.end();
        --it;
        if (it->fi >= A[i]) {
          M.erase(it);
        } else {
          break;
        }
      }

      int p = A[i] - 1;
      ++M[p];

      while (M[p] >= x) {
        M.erase(p);
        --p;
        if (p < 0) {
          return false;
        }
        ++M[p];
      }
    }
    cl = A[i];
  }
  return true;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> A[i]; }

  bool mon = 1;
  rep(i, N - 1) if (A[i] <= A[i - 1]) { mon = 0; }

  if (mon) {
    puts("1");
    return 0;
  }

  int lo = 1, hi = N;

  while (hi - lo > 1) {
    int m = (lo + hi) / 2;
    if (ok(m)) {
      hi = m;
    } else {
      lo = m;
    }
  }

  cout << hi << endl;
  return 0;
}

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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cout << #x << " = " << (x) << endl

const int MN = 200010;

int N;
int A[MN];

bool ok(int x) {
  set<pii> S;

  int cl = A[0];

  map<int, int> M;
  for (int i = 1; i < N; ++i) {
    if (A[i] <= cl) {
      while (!M.empty()) {
        auto it = M.end();
        --it;
        if (it->fi >= A[i]) {
          M.erase(it);
        } else {
          break;
        }
      }

      int p = A[i] - 1;
      ++M[p];

      while (M[p] >= x) {
        M.erase(p);
        --p;
        if (p < 0) {
          return false;
        }
        ++M[p];
      }
    }
    cl = A[i];
  }
  return true;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> A[i]; }

  bool mon = 1;
  rep(i, N - 1) if (A[i] <= A[i - 1]) { mon = 0; }

  if (mon) {
    puts("1");
    return 0;
  }

  int lo = 1, hi = N;

  while (hi - lo > 1) {
    int m = (lo + hi) / 2;
    if (ok(m)) {
      hi = m;
    } else {
      lo = m;
    }
  }

  cout << hi << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int N = 2e5 + 10;
int n, a[N], l = 1, r = N - 10, mid, ans;
map<int, int> p;
inline bool judge(int x) {
  p.clear();
  for (register int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    ++p[a[i]];
    map<int, int>::iterator it = p.upper_bound(a[i]);
    while (it != p.end())
      p.erase(it), it = p.upper_bound(a[i]);
    int pos = a[i];
    while (pos && p[pos] == x)
      p[pos] = 0, ++p[--pos];
    if (!pos)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  while (l <= r) {
    mid = (l + r + 1) >> 1;
    if (judge(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
const int N = 2e5 + 10;
int n, a[N], l = 1, r = N - 10, mid, ans;
map<int, int> p;
inline bool judge(int x) {
  p.clear();
  for (register int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    if (x == 1)
      return 0;
    ++p[a[i]];
    map<int, int>::iterator it = p.upper_bound(a[i]);
    while (it != p.end())
      p.erase(it), it = p.upper_bound(a[i]);
    int pos = a[i];
    while (pos && p[pos] == x)
      p[pos] = 0, ++p[--pos];
    if (!pos)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  while (l <= r) {
    mid = (l + r + 1) >> 1;
    if (judge(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int a[200100], n;

bool chk(int x) {
  mp.clear();
  for (int i = 1; i <= n; ++i)
    if (a[i] <= a[i - 1]) {
      for (auto it = mp.upper_bound(a[i]); it != mp.end();
           it = mp.upper_bound(a[i]))
        mp.erase(it);
      ++mp[a[i]];
      for (int j = a[i]; mp[j] >= x; --j) {
        if (j == 1)
          return 0;
        mp[j] -= x;
        mp[j - 1]++;
      }
    }
  return 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (chk(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int a[200100], n;

bool chk(int x) {
  mp.clear();
  for (int i = 1; i <= n; ++i)
    if (a[i] <= a[i - 1]) {
      for (auto it = mp.upper_bound(a[i]); it != mp.end();
           it = mp.upper_bound(a[i]))
        mp.erase(it);
      ++mp[a[i]];
      if (x == 1)
        return 0;
      for (int j = a[i]; mp[j] >= x; --j) {
        if (j == 1)
          return 0;
        mp[j] -= x;
        mp[j - 1]++;
      }
    }
  return 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (chk(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << '\n';
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define INF 1000000007
using namespace std;

int n, a[200010], cnt;
pair<int, int> q[200010];

inline bool check(int x) {
  if (x == 1) {
    bool flag = false;
    for (int i = 1; i < n; i++)
      if (a[i] >= a[i + 1]) {
        flag = true;
        break;
      }
    if (!flag)
      return true;
  }
  int pre = 0;
  map<int, int> mp;
  mp.clear();
  for (int i = 1; i <= n; i++) {
    if (a[i] <= pre) {
      pre = a[i];
      while (pre >= 1) {
        if (mp[pre] + 1 < x)
          break;
        mp[pre] = 0;
        pre--;
      }
      if (pre == 0)
        return false;
      mp[pre]++;
    } else
      pre = a[i];
    mp.erase(mp.upper_bound(pre), mp.end());
  }
  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 1, r = INF, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define INF 1000000007
using namespace std;

int n, a[200010], cnt;
pair<int, int> q[200010];

inline bool check(int x) {
  if (x == 1) {
    bool flag = false;
    for (int i = 1; i < n; i++)
      if (a[i] >= a[i + 1]) {
        flag = true;
        break;
      }
    if (!flag)
      return true;
    return false;
  }
  int pre = 0;
  map<int, int> mp;
  mp.clear();
  for (int i = 1; i <= n; i++) {
    if (a[i] <= pre) {
      pre = a[i];
      while (pre >= 1) {
        if (mp[pre] + 1 < x)
          break;
        mp[pre] = 0;
        pre--;
      }
      if (pre == 0)
        return false;
      mp[pre]++;
    } else
      pre = a[i];
    mp.erase(mp.upper_bound(pre), mp.end());
  }
  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = 1, r = INF, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return x * f;
}
const int N = 400010, INF = 0x3f3f3f3f;
int a[N], n;
struct DATA {
  int x, y;
} s[N];
int ls, mid;
inline void insert(int x) {
  if (s[ls].x > x) {
    while (s[ls].x > x)
      --ls;
    if (s[ls].x < x)
      s[++ls] = (DATA){x, 1};
  }
  if (s[ls].x == x) {
    ++s[ls].y;
    if (s[ls].y > mid)
      insert(x - 1), s[++ls] = (DATA){x, 1};
  } else
    s[++ls] = (DATA){x, 1};
}
inline bool judge() {
  ls = 0;
  s[++ls] = (DATA){0, 0};
  for (int i = 1; i <= n; ++i) {
    insert(a[i]);
    if (s[1].y)
      return 0;
  }
  return 1;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  int l = 1, r = n;
  while (l < r) {
    mid = (l + r) >> 1;
    if (judge())
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-')
      f = -1;
  } while (ch < '0' || ch > '9');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return x * f;
}
const int N = 400010, INF = 0x3f3f3f3f;
int a[N], n;
struct DATA {
  int x, y;
} s[N];
int ls, mid;
inline void insert(int x) {
  if (s[ls].x > x) {
    while (s[ls].x > x)
      --ls;
    if (s[ls].x < x)
      s[++ls] = (DATA){x, 1};
  }
  if (s[ls].x == x) {
    ++s[ls].y;
    if (s[ls].y > mid)
      insert(x - 1), s[++ls] = (DATA){x, 1};
  } else
    s[++ls] = (DATA){x, 1};
}
inline bool judge() {
  ls = 0;
  s[++ls] = (DATA){0, 0};
  for (int i = 1; i <= n; ++i) {
    if (mid == 1 && a[i] <= a[i - 1])
      return 0;
    insert(a[i]);
    if (s[1].y)
      return 0;
  }
  return 1;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  int l = 1, r = n;
  while (l < r) {
    mid = (l + r) >> 1;
    if (judge())
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, l, r, mid, ans;
int a[100005];
struct node {
  int u, l, r;
};

int check(int x) {
  stack<node> f;
  f.push((node){1, 1, a[1]});
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      f.push((node){1, a[i - 1] + 1, a[i]});
    else {
      int p;
      while (!f.empty()) {
        node v = f.top();
        if (v.l > a[i])
          f.pop();
        else {
          p = v.u;
          f.pop();
          if (v.l < a[i])
            f.push((node){p, v.l, a[i] - 1});
          break;
        }
      }
      if (p < x)
        f.push((node){p + 1, a[i], a[i]});
      else {
        while (!f.empty()) {
          node v = f.top();
          p = v.u;
          f.pop();
          if (p < x) {
            if (v.l != v.r)
              f.push((node){p, v.l, v.r - 1});
            f.push((node){p + 1, v.r, v.r});
            f.push((node){1, v.r + 1, a[i]});
            break;
          }
        }
        if (p >= x)
          return 0;
      }
    }
  }
  return 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  l = 1, r = 1e9, ans = 1e9 + 1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid))
      ans = min(ans, mid), r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, l, r, mid, ans;
int a[200005];
struct node {
  int u, l, r;
};

int check(int x) {
  stack<node> f;
  f.push((node){1, 1, a[1]});
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      f.push((node){1, a[i - 1] + 1, a[i]});
    else {
      int p;
      while (!f.empty()) {
        node v = f.top();
        if (v.l > a[i])
          f.pop();
        else {
          p = v.u;
          f.pop();
          if (v.l < a[i])
            f.push((node){p, v.l, a[i] - 1});
          break;
        }
      }
      if (p < x)
        f.push((node){p + 1, a[i], a[i]});
      else {
        while (!f.empty()) {
          node v = f.top();
          p = v.u;
          f.pop();
          if (p < x) {
            if (v.l != v.r)
              f.push((node){p, v.l, v.r - 1});
            f.push((node){p + 1, v.r, v.r});
            f.push((node){1, v.r + 1, a[i]});
            break;
          }
        }
        if (p >= x)
          return 0;
      }
    }
  }
  return 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  l = 1, r = 1e9, ans = 1e9 + 1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid))
      ans = min(ans, mid), r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03202 (C++)

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

int check(int m, vector<int> &A) {
  map<int, int> S;
  for (int i = 1; i < A.size(); i++) {
    if (A[i - 1] < A[i])
      continue;
    if (m == 0)
      return 0;
    int a = A[i];
    S.erase(S.upper_bound(a), S.end());
    while (S[a] == m - 1) {
      a--;
      if (a == 0)
        return 0;
    }
    S[a]++;
    S.erase(S.upper_bound(a), S.end());
  }
  return 1;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int &a : A)
    cin >> a;

  int l = 0, r = N;
  while (1 < r - l) {
    int m = (l + r) / 2;
    if (check(m, A))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
}

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

int check(int m, vector<int> &A) {
  map<int, int> S;
  for (int i = 1; i < A.size(); i++) {
    if (A[i - 1] < A[i])
      continue;
    if (m == 1)
      return 0;
    int a = A[i];
    S.erase(S.upper_bound(a), S.end());
    while (S[a] == m - 1) {
      a--;
      if (a == 0)
        return 0;
    }
    S[a]++;
    S.erase(S.upper_bound(a), S.end());
  }
  return 1;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int &a : A)
    cin >> a;

  int l = 0, r = N;
  while (1 < r - l) {
    int m = (l + r) / 2;
    if (check(m, A))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
}
Problem p03202 (C++)

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

const int MAX = 1000000;
const int MOD = 1000000007;

signed main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ok = n, ng = 0, mid, j;
  map<int, int> mp;
  while (abs(ok - ng) > 1) {
    mp.clear();
    mid = (ok + ng) / 2;
    for (int i = 1; i < n; i++) {
      if (a[i - 1] < a[i])
        continue;
      while (mp.size() > 0 && mp.rbegin()->first >= a[i])
        mp.erase((next(mp.rbegin())).base());
      // mp.erase(mp.lower_bound(a[i]),mp.end());
      j = a[i] - 1;
      mp[j]++;
      while (j > 0 && mp[j] == mid) {
        mp[j] = 0;
        j--;
        mp[j]++;
      }
      if (mp[0] >= mid)
        break;
    }
    if (mp[0] >= mid)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok;
  return 0;
}


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

const int MAX = 1000000;
const int MOD = 1000000007;

signed main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    if (a[i - 1] >= a[i])
      break;
    if (i == n - 1) {
      cout << 1;
      return 0;
    }
  }
  if (n == 1) {
    cout << 1;
    return 0;
  }
  int ok = n, ng = 1, mid, j;
  map<int, int> mp;
  while (abs(ok - ng) > 1) {
    mp.clear();
    mid = (ok + ng) / 2;
    for (int i = 1; i < n; i++) {
      if (a[i - 1] < a[i])
        continue;
      while (mp.size() > 0 && mp.rbegin()->first >= a[i])
        mp.erase((next(mp.rbegin())).base());
      // mp.erase(mp.lower_bound(a[i]),mp.end());
      j = a[i] - 1;
      mp[j]++;
      while (j > 0 && mp[j] == mid) {
        mp[j] = 0;
        j--;
        mp[j]++;
      }
      if (mp[0] >= mid)
        break;
    }
    if (mp[0] >= mid)
      ng = mid;
    else
      ok = mid;
  }
  cout << ok;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[200005];
int b[200005];
int n;
map<int, int> mp;
bool check(int mid) {
  mp.clear();
  for (int i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    mp.erase(mp.upper_bound(a[i]), mp.end());
    int j;
    for (j = a[i]; j > 0; j--) {
      if (mp[j] < mid) {
        mp[j]++;
        break;
      }
      mp.erase(mp.find(j));
    }
    if (j == 0)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 0, r = n, res = -1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (!check(mid)) {
      l = mid + 1;
    } else {
      res = mid;
      r = mid - 1;
    }
  }
  cout << res + 1;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[200005];
int b[200005];
int n;
map<int, int> mp;
bool check(int mid) {
  mp.clear();
  for (int i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    mp.erase(mp.upper_bound(a[i]), mp.end());
    int j;
    for (j = a[i]; j > 0; j--) {
      if (mp[j] < mid) {
        mp[j]++;
        break;
      }
      mp.erase(mp.find(j));
    }
    if (j == 0)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  bool flag = 0;
  for (int i = 0; i < n - 1; i++)
    if (a[i] >= a[i + 1])
      flag = 1;
  if (!flag) {
    cout << 1;
    return 0;
  }
  int l = 1, r = n, res = -1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (!check(mid)) {
      l = mid + 1;
    } else {
      res = mid;
      r = mid - 1;
    }
  }
  cout << res + 1;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector<int> a;

bool check(int x) {
  map<int, int> cnt;
  for (int i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (cnt.size() && a[i] < cnt.rbegin()->first) {
      cnt.erase(--cnt.end());
    }
    int cur = a[i];
    while (cur > 0 && cnt[cur] == x)
      cur--;
    if (cur == 0)
      return 0;
    cnt[cur]++;
    while (cnt.size() && cur < cnt.rbegin()->first) {
      cnt.erase(--cnt.end());
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  int l = 0, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
vector<int> a;

bool check(int x) {
  map<int, int> cnt;
  for (int i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (cnt.size() && a[i] < cnt.rbegin()->first) {
      cnt.erase(--cnt.end());
    }
    int cur = a[i];
    while (cur > 0 && cnt[cur] == x)
      cur--;
    if (cur == 0)
      return 0;
    cnt[cur]++;
    while (cnt.size() && cur < cnt.rbegin()->first) {
      cnt.erase(--cnt.end());
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  bool ok = 1;
  for (int i = 1; i < n; i++) {
    ok &= (a[i] > a[i - 1]);
  }
  if (ok)
    finish(1);
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l + 1 << endl;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 5;
int n, a[N];
bool solve(int m) {
  stack<pair<int, int>> s;
  for (int i = 1; i <= n; i++) {
    if (s.empty())
      s.push({a[i], 1});
    else {
      if (a[i] > s.top().fi)
        s.push({a[i], 1});
      else {
        if (m == 1)
          return false;
        while (!s.empty() && s.top().fi > a[i])
          s.pop();
        if (s.empty() || s.top().fi < a[i])
          s.push({a[i], 2});
        else {
          int now = s.top().fi;
          while (!s.empty() && s.top().fi == now && s.top().se == m) {
            now--;
            s.pop();
          }
          if (s.empty() && now == 0)
            return false;
          if (s.top().fi == now) {
            int p = s.top().se + 1;
            s.pop();
            s.push({now, p});
          } else
            s.push({now, 2});
        }
      }
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = n, ans;
  while (l <= r) {
    int m = l + r >> 1;
    if (solve(m))
      ans = m, r = m - 1;
    else
      l = m + 1;
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 5;
int n, a[N];
bool solve(int m) {
  stack<pair<int, int>> s;
  for (int i = 1; i <= n; i++) {
    if (s.empty())
      s.push({a[i], 1});
    else {
      if (a[i] > s.top().fi)
        s.push({a[i], 1});
      else {
        if (m == 1)
          return false;
        while (!s.empty() && s.top().fi > a[i])
          s.pop();
        if (s.empty() || s.top().fi < a[i])
          s.push({a[i], 2});
        else {
          int now = s.top().fi;
          while (!s.empty() && s.top().fi == now && s.top().se == m) {
            now--;
            s.pop();
          }
          if (s.empty() && now == 0)
            return false;
          if (!s.empty() && s.top().fi == now) {
            int p = s.top().se + 1;
            s.pop();
            s.push({now, p});
          } else
            s.push({now, 2});
        }
      }
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int l = 1, r = n, ans;
  while (l <= r) {
    int m = l + r >> 1;
    if (solve(m))
      ans = m, r = m - 1;
    else
      l = m + 1;
  }
  printf("%d\n", ans);
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int check(int x) {
  if (x == 1)
    return is_sorted(A.begin(), A.end());
  map<int, int> m;
  for (int i = 1; i < A.size(); i++) {
    if (A[i - 1] < A[i])
      continue;
    m.erase(m.upper_bound(A[i]));
    int k = A[i];
    m[k]++;
    while (k > 0 && m[k] == x)
      m.erase(k), m[--k]++;
    if (k == 0)
      return false;
  }
  return true;
}
int main(void) {
  int N;
  cin >> N;
  A.resize(N);
  for (int &a : A)
    cin >> a;
  int l = 0, h = N;
  while (h - l > 1) {
    int m = (l + h) / 2;
    if (check(m))
      h = m;
    else
      l = m;
  }
  cout << h << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int check(int x) {
  if (x == 1)
    return is_sorted(A.begin(), A.end());
  map<int, int> m;
  for (int i = 1; i < A.size(); i++) {
    if (A[i - 1] < A[i])
      continue;
    if (m.upper_bound(A[i]) != m.end())
      m.erase(m.upper_bound(A[i]));
    int k = A[i];
    m[k]++;
    while (k > 0 && m[k] == x)
      m.erase(k), m[--k]++;
    if (k == 0)
      return false;
  }
  return true;
}
int main(void) {
  int N;
  cin >> N;
  A.resize(N);
  for (int &a : A)
    cin >> a;
  int l = 0, h = N;
  while (h - l > 1) {
    int m = (l + h) / 2;
    if (check(m))
      h = m;
    else
      l = m;
  }
  cout << h << endl;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200200;
int n;
int a[N];
map<int, int> st;
bool test(int cnt) {
  st.clear();
  for (int i = 1; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      int j = a[i];
      while (++st[j] == cnt) {
        j--;
        if (j == 0) {
          return false;
        }
      }
      while (!st.empty() && (--st.end())->first > j) {
        st.erase(--st.end());
      }
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  a[0] = 0;
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (test(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 200200;
int n;
int a[N];
map<int, int> st;
bool test(int cnt) {
  st.clear();
  for (int i = 1; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      int j = a[i];
      while (++st[j] == cnt) {
        j--;
        if (j == 0 || cnt == 1) {
          return false;
        }
      }
      while (!st.empty() && (--st.end())->first > j) {
        st.erase(--st.end());
      }
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  a[0] = 0;
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (test(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define LL long long
#define fgx cerr << "--------------" << endl;
#define dgx cerr << "==============" << endl;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 100010;
const int INF = 2147483600;

#define op first
#define ed second
int N;
int a[MAXN + 1];
pair<int, int> s[MAXN + 1];
int top;

inline bool check(int k) {
  top = 0;
  for (int i = 1; i <= N; i++) {
    int len = a[i];
    if (s[top].ed < len) {
      s[++top] = make_pair(1, len);
      continue;
    }
    while (top > 1 && s[top - 1].ed >= len)
      --top;
    s[top].ed = min(s[top].ed, len);
    while (top && s[top].op == k)
      --top;
    if (!top)
      return 0;
    int ps = s[top].ed, val = s[top].op;
    if (ps - 1 == s[top - 1].ed)
      --top;
    if (ps > 1)
      s[top].ed = ps - 1;
    s[++top] = make_pair(val + 1, ps);
    if (len > ps)
      s[++top] = make_pair(1, len);
  }
  return 1;
}
int ans;

int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  N = read();
  for (int i = 1; i <= N; i++)
    a[i] = read();
  int l = 1, r = N;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define LL long long
#define fgx cerr << "--------------" << endl;
#define dgx cerr << "==============" << endl;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 200010;
const int INF = 2147483600;

#define op first
#define ed second
int N;
int a[MAXN + 1];
pair<int, int> s[MAXN + 1];
int top;

inline bool check(int k) {
  top = 0;
  for (int i = 1; i <= N; i++) {
    int len = a[i];
    if (s[top].ed < len) {
      s[++top] = make_pair(1, len);
      continue;
    }
    while (top > 1 && s[top - 1].ed >= len)
      --top;
    s[top].ed = min(s[top].ed, len);
    while (top && s[top].op == k)
      --top;
    if (!top)
      return 0;
    int ps = s[top].ed, val = s[top].op;
    if (ps - 1 == s[top - 1].ed)
      --top;
    if (ps > 1)
      s[top].ed = ps - 1;
    s[++top] = make_pair(val + 1, ps);
    if (len > ps)
      s[++top] = make_pair(1, len);
  }
  return 1;
}
int ans;

int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  N = read();
  for (int i = 1; i <= N; i++)
    a[i] = read();
  int l = 1, r = N;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
// ΔAGC029C
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 222222;
int n, a[N];
map<int, int> M;
set<int> S;
int ok(int x) {
  int i, t = 0;
  M.clear();
  S.clear();
  for (i = 1; i <= n; i = i + 1) {
    if (a[i] > a[i - 1])
      continue;
    t = a[i];
    while (!S.empty() && *S.rbegin() > t) {
      M[*S.rbegin()] = 0;
      S.erase(*S.rbegin());
    }
    while (M[t] == x - 1) {
      M[t] = 0;
      S.erase(t);
      t--;
    }
    if (t <= 0)
      return 0;
    M[t]++;
    S.insert(t);
  }
  return 1;
}
int main() {
  int i, l, r;
  scanf("%d", &n);
  for (i = 1; i <= n; i = i + 1)
    scanf("%d", a + i);
  for (i = 2; i <= n; i = i + 1)
    if (a[i - 1] >= a[i])
      break;
  if (i > n) {
    cout << 1;
    return 0;
  }
  l = 2, r = n;
  while (l < r) {
    i = l + r >> 1;
    if (ok(i))
      r = i;
    else
      l = i + 1;
  }
  cout << l;
  return 0;
}

Fixed submission:
// ΔAGC029C
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 222222;
int n, a[N];
map<int, int> M;
set<int> S;
int ok(int x) {
  int i, t = 0;
  M.clear();
  S.clear();
  for (i = 1; i <= n; i = i + 1) {
    if (a[i] > a[i - 1])
      continue;
    t = a[i];
    while (!S.empty() && *S.rbegin() > t) {
      M[*S.rbegin()] = 0;
      S.erase(*S.rbegin());
    }
    while (M[t] == x - 1) {
      M[t] = 0;
      S.erase(t);
      t--;
    }
    if (t <= 0)
      return 0;
    M[t]++;
    S.insert(t);
  }
  return 1;
}
int main() {
  int i, l, r;
  scanf("%d", &n);
  for (i = 1; i <= n; i = i + 1)
    scanf("%d", a + i);
  for (i = 2; i <= n; i = i + 1)
    if (a[i - 1] >= a[i])
      break;
  if (i > n) {
    cout << 1;
    return 0;
  }
  for (i = 2; i <= 5; i = i + 1) {
    if (ok(i)) {
      cout << i;
      return 0;
    }
  }
  l = 6, r = n;
  while (l < r) {
    i = l + r >> 1;
    if (ok(i))
      r = i;
    else
      l = i + 1;
  }
  cout << l;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int s[100];
int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  bool inc = true;
  rep(i, n - 1) if (a[i] >= a[i + 1]) inc = false;
  if (inc) {
    cout << 1 << endl;
    return 0;
  }
  int ng = 1, ok = n;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    rep(i, min(a[0], 100)) s[i] = 1;
    REP(i, a[0], 100) s[i] = 0;
    bool tempura = true;
    rep(i, n - 1) {
      if (a[i + 1] > a[i])
        REP(j, a[i], min(100, a[i + 1])) s[j] = 1;
      else {
        if (a[i + 1] > 100)
          continue;
        REP(j, a[i + 1], a[i]) s[j] = 0;
        bool can = false;
        for (int j = a[i + 1] - 1; j >= 0; --j) {
          if (s[j] < mid) {
            ++s[j];
            REP(k, j + 1, a[i + 1]) s[k] = 1;
            can = true;
            break;
          }
        }
        if (!can) {
          tempura = false;
          break;
        }
      }
    }
    if (tempura)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

int s[100];
int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  bool inc = true;
  rep(i, n - 1) if (a[i] >= a[i + 1]) inc = false;
  if (inc) {
    cout << 1 << endl;
    return 0;
  }
  int ng = 1, ok = n;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    rep(i, min(a[0], 100)) s[i] = 1;
    REP(i, a[0], 100) s[i] = 0;
    bool tempura = true;
    rep(i, n - 1) {
      if (a[i + 1] > a[i])
        REP(j, a[i], min(100, a[i + 1])) s[j] = 1;
      else {
        if (a[i + 1] > 100)
          continue;
        REP(j, a[i + 1], min(100, a[i])) s[j] = 0;
        bool can = false;
        for (int j = a[i + 1] - 1; j >= 0; --j) {
          if (s[j] < mid) {
            ++s[j];
            REP(k, j + 1, a[i + 1]) s[k] = 1;
            can = true;
            break;
          }
        }
        if (!can) {
          tempura = false;
          break;
        }
      }
    }
    if (tempura)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector
#define INF 1000000007

void p(vec<int> a) {
  rep(i, a.size()) cout << a[i] << endl;
  return;
}

bool check(vec<int> a, int k) {
  map<int, int> s;
  s[a[0] - 1] = 1;
  rep(i, a.size() - 1) {
    if (a[i + 1] > a[i]) {
      s[a[i + 1] - 1] = 1;
    } else {
      int cur = a[i + 1] - 1;
      while (cur >= 0) {
        int temp = s.find(cur) == end(s) ? 1 : s[cur];
        temp++;
        if (temp <= k) {
          s[cur] = temp;
          break;
        }
        s[cur] = 1;
        cur--;
      }
      if (cur < 0)
        return false;
    }
    auto it = s.find(a[i + 1] - 1);
    it++;
    s.erase(it, end(s));
  }
  return true;
}

int main(void) {
  int n;
  cin >> n;
  vec<int> a(n);
  rep(i, n) cin >> a[i];

  int left = 1, right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (check(a, mid))
      right = mid;
    else
      left = mid + 1;
  }
  cout << left << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector
#define INF 1000000007

void p(vec<int> a) {
  rep(i, a.size()) cout << a[i] << endl;
  return;
}

bool check(vec<int> a, int k) {
  map<int, int> s;
  s[a[0] - 1] = 1;
  rep(i, a.size() - 1) {
    if (a[i + 1] > a[i]) {
      s[a[i + 1] - 1] = 1;
    } else {
      if (k == 1)
        return false;
      int cur = a[i + 1] - 1;
      while (cur >= 0) {
        int temp = s.find(cur) == end(s) ? 1 : s[cur];
        temp++;
        if (temp <= k) {
          s[cur] = temp;
          break;
        }
        s[cur] = 1;
        cur--;
      }
      if (cur < 0)
        return false;
    }
    auto it = s.find(a[i + 1] - 1);
    it++;
    s.erase(it, end(s));
  }
  return true;
}

int main(void) {
  int n;
  cin >> n;
  vec<int> a(n);
  rep(i, n) cin >> a[i];

  int left = 1, right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (check(a, mid))
      right = mid;
    else
      left = mid + 1;
  }
  cout << left << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\n'
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const ll inf = 1e9 + 7;
const ll mod = 998244353;
signed main() {
  int n;
  cin >> n;
  int ng = -1, ok = n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  while (ok - ng > 1) {
    int mid = ng + (ok - ng) / 2;
    map<ll, ll> ma;
    bool f = 1;
    int la = 0;
    rep(i, n) {
      if (la < a[i]) {
        la = a[i];
      } else {
        for (auto e : ma) {
          if (e.fi >= -a[i])
            break;
          ma.erase(e.fi);
        }
        ll now = 1;
        ll z = -a[i];
        while (now == 1) {
          if (ma[z] == mid && z < 0) {
            ma.erase(z);
            z++;
          } else {
            ma[z]++;
            now = 0;
          }
        }
        if (ma[0] > 0) {
          f = 0;
          break;
        }
        la = a[i];
      }
    }
    if (f)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\n'
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const ll inf = 1e9 + 7;
const ll mod = 998244353;
signed main() {
  int n;
  cin >> n;
  int ng = -1, ok = n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  while (ok - ng > 1) {
    int mid = ng + (ok - ng) / 2;
    map<ll, ll> ma;
    bool f = 1;
    int la = 0;
    rep(i, n) {
      if (la < a[i]) {
        la = a[i];
      } else {
        if (mid == 0) {
          f = 0;
          break;
        }
        for (auto e : ma) {
          if (e.fi >= -a[i])
            break;
          ma.erase(e.fi);
        }
        ll now = 1;
        ll z = -a[i];
        while (now == 1) {
          if (ma[z] == mid && z < 0) {
            ma.erase(z);
            z++;
          } else {
            ma[z]++;
            now = 0;
          }
        }
        if (ma[0] > 0) {
          f = 0;
          break;
        }
        la = a[i];
      }
    }
    if (f)
      ok = mid;
    else
      ng = mid;
  }
  cout << ok + 1 << endl;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
bool test(const vector<int> &A, int k) {
  int last = 0;
  map<int, int> m;
  for (int a : A) {
    if (a <= last) {
      m[a] += 1;
      m.erase(++m.find(a), m.end());
      int b = a;
      while (b > 0 && m[b] >= k) {
        m[b] = 0;
        m[b - 1]++;
        b--;
      }
      if (b == 0)
        return false;
    }
    last = a;
  }
  return true;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A)
    cin >> a;
  int lo = 0, hi = N;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    (test(A, mid) ? hi : lo) = mid;
  }
  cout << hi << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
bool test(const vector<int> &A, int k) {
  int last = 0;
  if (k == 1) {
    for (int a : A) {
      if (a <= last)
        return false;
      last = a;
    }
    return true;
  }
  map<int, int> m;
  for (int a : A) {
    if (a <= last) {
      m[a] += 1;
      m.erase(++m.find(a), m.end());
      int b = a;
      while (b > 0 && m[b] >= k) {
        m[b] = 0;
        m[b - 1]++;
        b--;
      }
      if (b == 0)
        return false;
    }
    last = a;
  }
  return true;
}
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A)
    cin >> a;
  int lo = 0, hi = N;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    (test(A, mid) ? hi : lo) = mid;
  }
  cout << hi << endl;
  return 0;
}
Problem p03202 (C++)

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

using namespace std;

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// cout << fixed << setprecision(20);

int N;
int A[220000];

signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int lb = 0, ub = N + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    map<int, int> mp;
    for (int i = 1; i < N; i++) {
      if (A[i - 1] >= A[i]) {
        for (auto it = mp.upper_bound(A[i]);
             it->first <= A[i - 1] && it != mp.end();
             it = mp.upper_bound(A[i])) {
          mp.erase(it);
        }
        for (int j = A[i]; j > 0; j--) {
          if (mp[j] == mid - 1)
            mp.erase(j);
          else {
            mp[j]++;
            goto umiko;
          }
        }
        lb = mid;
        goto nene;
      }
    umiko:;
    }
    ub = mid;
  nene:;
  }
  cout << ub << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// cout << fixed << setprecision(20);

int N;
int A[220000];

signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 1; i < N; i++)
    if (A[i - 1] >= A[i])
      goto aoba;
  cout << 1 << endl;
  return 0;
aoba:;
  int lb = 1, ub = N + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    map<int, int> mp;
    for (int i = 1; i < N; i++) {
      if (A[i - 1] >= A[i]) {
        for (auto it = mp.upper_bound(A[i]);
             it->first <= A[i - 1] && it != mp.end();
             it = mp.upper_bound(A[i])) {
          mp.erase(it);
        }
        for (int j = A[i]; j > 0; j--) {
          if (mp[j] == mid - 1)
            mp.erase(j);
          else {
            mp[j]++;
            goto umiko;
          }
        }
        lb = mid;
        goto nene;
      }
    umiko:;
    }
    ub = mid;
  nene:;
  }
  cout << ub << endl;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
struct node {
  map<int, int> s;
  inline void clear(int x) {
    while (s.size() && (--s.end())->first >= x)
      s.erase(--s.end());
  }
  inline int &operator[](int x) {
    if (!s.count(x))
      s[x] = 0;
    return s[x];
  }
} s;
int n, a[200010];
inline int check(int x) {
  s.clear(0);
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    s.clear(a[i]);
    int now = a[i] - 1;
    while (now >= 0) {
      if (++s[now] != x)
        break;
      s[now] = 0;
      now--;
    }
    if (now < 0)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  int l = 1, r = n, ans;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return cout << ans << endl, 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct node {
  map<int, int> s;
  inline void clear(int x) {
    while (s.size() && (--s.end())->first >= x)
      s.erase(--s.end());
  }
  inline int &operator[](int x) {
    if (!s.count(x))
      s[x] = 0;
    return s[x];
  }
} s;
int n, a[200010];
inline int check(int x) {
  s.clear(0);
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      continue;
    s.clear(a[i]);
    int now = a[i] - 1;
    while (now >= 0) {
      if (++s[now] != x)
        break;
      s[now] = 0;
      now--;
    }
    if (now < 0)
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  int tag = 1;
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      tag = 0;
  if (tag)
    return puts("1"), 0;
  int l = 2, r = n, ans;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return cout << ans << endl, 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

vector<long long> a;

bool check(int k) {
  map<int, int> mp;
  for (int i = 1; i < a.size(); i++) {
    if (a[i] <= a[i - 1]) {
      int p = a[i];
      mp.erase(mp.upper_bound(p), mp.end());
      while (mp[p] == k - 1) {
        p--;
        if (p == 0)
          return false;
      }
      mp[p]++;
      mp.erase(mp.upper_bound(p), mp.end());
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = 0, r = 200010;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

vector<long long> a;

bool check(int k) {
  map<int, int> mp;
  for (int i = 1; i < a.size(); i++) {
    if (a[i] > a[i - 1])
      continue;
    if (k == 1)
      return false;
    if (a[i] <= a[i - 1]) {
      int p = a[i];
      mp.erase(mp.upper_bound(p), mp.end());
      while (mp[p] == k - 1) {
        p--;
        if (p == 0)
          return false;
      }
      mp[p]++;
      mp.erase(mp.upper_bound(p), mp.end());
    }
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = 0, r = 200010;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
  return 0;
}