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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int mx = 200005;
int n, a[mx], l, r = mx;

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d", a + i);
  while (l + 1 < r) {
    int mid = (l + r) / 2, hight = 0;
    bool ok = true;
    map<int, int> mp;
    rep(i, n) {
      if (hight < a[i])
        hight = a[i];
      else {
        hight = a[i];
        while (++mp[hight] >= mid && hight > 0)
          hight--;
        if (hight == 0) {
          ok = false;
          break;
        }
      }
      mp.erase(mp.upper_bound(hight), mp.end());
    }
    (ok ? r : l) = mid;
  }
  printf("%d\n", r);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int mx = 200005;
int n, a[mx], l, r = mx;

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d", a + i);
  while (l + 1 < r) {
    int mid = (l + r) / 2, hight = 0;
    bool ok = true;
    map<int, int> mp;
    rep(i, n) {
      if (hight < a[i])
        hight = a[i];
      else {
        if (mid == 1) {
          ok = false;
          break;
        }
        hight = a[i];
        while (++mp[hight] >= mid && hight > 0)
          hight--;
        if (hight == 0) {
          ok = false;
          break;
        }
      }
      mp.erase(mp.upper_bound(hight), mp.end());
    }
    (ok ? r : l) = mid;
  }
  printf("%d\n", r);
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 1000000007;
int inf = 1000001000;
ll INF = 100000000000000000;

int main() {
  int n;
  cin >> n;
  int l = 1, r = n;
  vector<int> a(n), b(0);
  rep(i, n) {
    cin >> a.at(i);
    if (i != 0) {
      if (a.at(i) <= a.at(i - 1))
        b.push_back(a.at(i));
    }
  }
  while (l != r) {
    int mid = (l + r) / 2;
    map<int, int> m;
    bool t = false;
    rep(i, b.size()) {
      while (!m.empty() && m.rbegin()->first > b[i])
        m.erase(prev(m.end()));

      int x = b[i];
      m[x]++;
      if (mid == 1)
        t = true;
      while (m[x] == mid) {
        m.erase(m.find(x));
        x--;
        m[x]++;
        if (x <= 0) {
          t = true;
          break;
        }
      }
      if (x <= 0)
        t = true;
    }
    if (t)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 1000000007;
int inf = 1000001000;
ll INF = 100000000000000000;

int main() {
  int n;
  cin >> n;
  int l = 1, r = n;
  vector<int> a(n), b(0);
  rep(i, n) {
    cin >> a.at(i);
    if (i != 0) {
      if (a.at(i) <= a.at(i - 1))
        b.push_back(a.at(i));
    }
  }
  while (l != r) {
    int mid = (l + r) / 2;
    map<int, int> m;
    bool t = false;
    rep(i, b.size()) {
      while (!m.empty() && m.rbegin()->first > b[i])
        m.erase(prev(m.end()));

      int x = b[i];
      m[x]++;
      if (mid == 1)
        t = true;
      if (t)
        break;
      while (m[x] == mid) {
        m.erase(m.find(x));
        x--;
        m[x]++;
        if (x <= 0) {
          t = true;
          break;
        }
      }
      if (x <= 0)
        t = true;
    }
    if (t)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
}
Problem p03202 (C++)

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

bool can(int n, const vector<int> &a, int x) {
  map<int, int> ma;
  for (int i = 1; i < n; ++i) {
    if (a[i] > a[i - 1])
      continue;
    int k = a[i];
    auto p = ma.upper_bound(k);
    if (p != ma.end())
      ma.erase(p);
    ++ma[k];
    while (k > 0 && ma[k] == x) {
      ma.erase(k);
      ++ma[--k];
    }
    if (k == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  int low = 0, high = n + 1;
  while (low + 1 < high) {
    int mid = (low + high) / 2;
    (can(n, a, mid) ? high : low) = mid;
  }
  printf("%d\n", high);
  return 0;
}

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

bool can(int n, const vector<int> &a, int x) {
  if (x == 1)
    for (int i = 1; i < n; ++i)
      if (a[i] <= a[i - 1])
        return false;
  map<int, int> ma;
  for (int i = 1; i < n; ++i) {
    if (a[i] > a[i - 1])
      continue;
    int k = a[i];
    auto p = ma.upper_bound(k);
    if (p != ma.end())
      ma.erase(p);
    ++ma[k];
    while (k > 0 && ma[k] == x) {
      ma.erase(k);
      ++ma[--k];
    }
    if (k == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  int low = 0, high = n + 1;
  while (low + 1 < high) {
    int mid = (low + high) / 2;
    (can(n, a, mid) ? high : low) = mid;
  }
  printf("%d\n", high);
  return 0;
}
Problem p03202 (C++)

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

bool f(const long kind, const size_t n, const vector<long> &a) {
  map<long, long> c;
  for (size_t i = 0; i < n - 1; i++) {
    if (a[i] >= a[i + 1]) {
      for_each(c.lower_bound(a[i + 1]), c.end(),
               [](pair<const long, long> &x) { x.second = 0; });
      long j = a[i + 1] - 1;
      c[j]++;
      for (;;) {
        if (c[j] < kind) {
          break;
        } else if (j <= 0) {
          return false;
        } else {
          c[j] = 0;
          c[j - 1]++;
          j--;
        }
      }
    }
  }
  return true;
}

bool strict_increasing(const vector<long> &a) {
  size_t n = a.size();
  for (size_t i = 1; i < n; i++) {
    if (a[i - 1] >= a[i]) {
      return false;
    }
  }
  return true;
}

int main(void) {
  long n;
  cin >> n;
  vector<long> a(n);
  for (long i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (strict_increasing(a)) {
    cout << 1 << endl;
    return 0;
  }
  long left = 1, right = n;
  for (;;) {
    const long mid = (left + right) / 2;
    if (right - left <= 1) {
      if (f(right, n, a)) {
        cout << right << endl;
      } else {
        cout << left << endl;
      }
      return 0;
    }
    const bool b = f(mid, n, a);
    // printf("[%ld, %ld] %ld %s\n", left, right, mid, b ? "true" : "false");
    if (b) {
      right = mid;
    } else {
      left = mid;
    }
  }
}


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

bool f(const long kind, const size_t n, const vector<long> &a) {
  map<long, long> c;
  for (size_t i = 1; i < n; i++) {
    if (a[i - 1] >= a[i]) {
      c.erase(c.lower_bound(a[i]), c.end());
      long j = a[i] - 1;
      c[j]++;
      for (;;) {
        if (c[j] < kind) {
          break;
        } else if (j <= 0) {
          return false;
        } else {
          c[j] = 0;
          c[j - 1]++;
          j--;
        }
      }
    }
  }
  return true;
}

bool strict_increasing(const vector<long> &a) {
  size_t n = a.size();
  for (size_t i = 1; i < n; i++) {
    if (a[i - 1] >= a[i]) {
      return false;
    }
  }
  return true;
}

int main(void) {
  long n;
  cin >> n;
  vector<long> a(n);
  for (long i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (strict_increasing(a)) {
    cout << 1 << endl;
    return 0;
  }
  long left = 1, right = n;
  for (;;) {
    const long mid = (left + right) / 2;
    if (right - left <= 1) {
      if (f(right, n, a)) {
        cout << right << endl;
      } else {
        cout << left << endl;
      }
      return 0;
    }
    const bool b = f(mid, n, a);
    // printf("[%ld, %ld] %ld %s\n", left, right, mid, b ? "true" : "false");
    if (b) {
      right = mid;
    } else {
      left = mid;
    }
  }
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)

using namespace std;
typedef long long int ll;
ll A[200000];
int N;
bool C(ll x) {
  map<ll, int> m;
  rep(i, 1, N) {
    if (A[i] <= A[i - 1]) {
      rer(j, A[i - 1] + 1, A[i] + 1) { m.erase(j); }
      if (m.find(A[i]) == m.end()) {
        m.insert(make_pair(A[i], 1));
        if (x == 1)
          return false;
      } else {
        m[A[i]]++;
        ll j = A[i];
        while (m[j] == x) {
          if (j == 1)
            return false;
          m[j] = 0;
          if (m.find(j - 1) == m.end())
            m.insert(make_pair(j - 1, 1));
          else
            m[j - 1]++;
          j--;
        }
      }
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, 0, N) cin >> A[i];
  ll l = 0, r = 2 * 1e5;
  while (r - l > 1) {
    ll mid = (l + r) / 2;
    if (C(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)

using namespace std;
typedef long long int ll;
ll A[200000];
int N;
bool C(ll x) {
  map<ll, int> m;
  rep(i, 1, N) {
    if (A[i] <= A[i - 1]) {
      m.erase(m.upper_bound(A[i]), m.end());
      if (m.find(A[i]) == m.end()) {
        m.insert(make_pair(A[i], 1));
        if (x == 1)
          return false;
      } else {
        m[A[i]]++;
        ll j = A[i];
        while (m[j] == x) {
          if (j == 1)
            return false;
          m[j] = 0;
          if (m.find(j - 1) == m.end())
            m.insert(make_pair(j - 1, 1));
          else
            m[j - 1]++;
          j--;
        }
      }
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, 0, N) cin >> A[i];
  ll l = 0, r = 2 * 1e5;
  while (r - l > 1) {
    ll mid = (l + r) / 2;
    if (C(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << "\n";
}
Problem p03202 (C++)

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

#define N 100010

using namespace std;

typedef long long ll;

char *p1, *p2, buf[100000];

#define nc()                                                                   \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)      \
       ? EOF                                                                   \
       : *p1++)

int rd() {
  int x = 0, f = 1;
  char c = nc();
  while (c < 48) {
    if (c == '-')
      f = -1;
    c = nc();
  }
  while (c > 47) {
    x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
  }
  return x * f;
}

int a[N], n;

map<ll, ll> MP;

bool check(int x) {
  MP.clear();
  for (int i = 2; i <= n; i++) {
    if (a[i - 1] >= a[i]) {
      if (x == 1) {
        return false;
      }
      while (!MP.empty()) {
        ll mx = MP.rbegin()->first;
        if (mx > a[i]) {
          MP.erase(mx);
        } else {
          break;
        }
      }

      int j = a[i];
      while (j > 0 && MP[j] + 1 == x) {
        MP.erase(j);
        j--;
      }
      if (!j) {
        return false;
      }
      MP[j]++;
    }
  }
  return true;
}

int main() {
  n = rd();
  for (int i = 1; i <= n; i++) {
    a[i] = rd();
  }

  int l = 1, r = n, ans = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}

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

#define N 200010

using namespace std;

typedef long long ll;

char *p1, *p2, buf[100000];

#define nc()                                                                   \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)      \
       ? EOF                                                                   \
       : *p1++)

int rd() {
  int x = 0, f = 1;
  char c = nc();
  while (c < 48) {
    if (c == '-')
      f = -1;
    c = nc();
  }
  while (c > 47) {
    x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
  }
  return x * f;
}

int a[N], n;

map<ll, ll> MP;

bool check(int x) {
  MP.clear();
  for (int i = 2; i <= n; i++) {
    if (a[i - 1] >= a[i]) {
      if (x == 1) {
        return false;
      }
      while (!MP.empty()) {
        ll mx = MP.rbegin()->first;
        if (mx > a[i]) {
          MP.erase(mx);
        } else {
          break;
        }
      }

      int j = a[i];
      while (j > 0 && MP[j] + 1 == x) {
        MP.erase(j);
        j--;
      }
      if (!j) {
        return false;
      }
      MP[j]++;
    }
  }
  return true;
}

int main() {
  n = rd();
  for (int i = 1; i <= n; i++) {
    a[i] = rd();
  }

  int l = 1, r = n, ans = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, a[200005];
int lt = 1, rt, md, ans;

int f(int p) {
  int i, t;
  map<int, int> mp;
  for (i = 2; i <= n; i++)
    if (a[i] <= a[i - 1]) {
      t = a[i];
      mp.erase(mp.upper_bound(t), mp.end());
      for (; t && mp[t] == p - 1; t--)
        mp.erase(t);
      if (!t)
        return 0;
      else
        mp[t]++;
    }
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  rt = n;
  while (lt <= rt) {
    md = lt + rt >> 1;
    if (f(md))
      ans = md, rt = md - 1;
    else
      lt = md + 1;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, a[200005];
int lt = 1, rt, md, ans;

int f(int p) {
  int i, t;
  map<int, int> mp;
  if (p == 1) {
    for (i = 2; i <= n; i++)
      if (a[i] <= a[i - 1])
        return 0;
    return 1;
  }
  for (i = 2; i <= n; i++)
    if (a[i] <= a[i - 1]) {
      t = a[i];
      mp.erase(mp.upper_bound(t), mp.end());
      for (; t && mp[t] == p - 1; t--)
        mp.erase(t);
      if (!t)
        return 0;
      else
        mp[t]++;
    }
  return 1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  rt = n;
  while (lt <= rt) {
    md = lt + rt >> 1;
    if (f(md))
      ans = md, rt = md - 1;
    else
      lt = md + 1;
  }
  cout << ans;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int N = 2e5 + 5;
int n, top;
int a[N];
struct node {
  int v, c;
  node(int v = 0, int c = 0) : v(v), c(c) {}
} st[N];
void insert(int v, int x) {
  while (st[top].v > v)
    top--;
  if (st[top].v == v)
    st[top].c++;
  else
    st[++top] = node(v, 1);
  if (top > 1 && st[top].c == x)
    top--, insert(v - 1, x);
}
inline bool check(int mid) {
  st[top = 1] = node();
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      insert(a[i], mid);
  return st[1].c == 0;
}
int main() {
  n = read();
  a[1] = read();
  int cnt = 0;
  for (int i = 2; i <= n; i++)
    a[i] = read(), cnt += a[i] <= a[i - 1];
  //	if(!cnt) return puts("1") , 0;
  int l = 1, r = n, ans = 0;
  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>
using namespace std;
typedef long long ll;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int N = 2e5 + 5;
int n, top;
int a[N];
struct node {
  int v, c;
  node(int v = 0, int c = 0) : v(v), c(c) {}
} st[N];
void insert(int v, int x) {
  while (st[top].v > v)
    top--;
  if (st[top].v == v)
    st[top].c++;
  else
    st[++top] = node(v, 1);
  if (top > 1 && st[top].c == x)
    top--, insert(v - 1, x);
}
inline bool check(int mid) {
  st[top = 1] = node();
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      insert(a[i], mid);
  return st[1].c == 0;
}
int main() {
  n = read();
  a[1] = read();
  int cnt = 0;
  for (int i = 2; i <= n; i++)
    a[i] = read(), cnt += a[i] <= a[i - 1];
  if (!cnt)
    return puts("1"), 0;
  int l = 2, r = n, ans = 0;
  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):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define all(a) (a).begin(), (a).end()
#define debug(x) cerr << (#x) << ": " << (x) << endl

int n;
vector<int> a;

void show(map<int, int> mp, int size) {
  rep1(i, size) { cout << char('a' + mp[i]); }
  cout << endl;
}

int check(int m) {
  // cout << char('a'+m-1) << "まで" << endl;
  map<int, int> mp;
  rep1(i, n - 1) {
    if (a[i - 1] < a[i])
      continue;
    if (m == 0)
      return false;
    int pos = a[i];
    mp.erase(mp.upper_bound(pos), mp.end());
    while (true) {
      if (mp[pos] < m - 1) {
        mp[pos]++;
        break;
      }
      mp.erase(--mp.end());
      pos--;
      if (pos == 0)
        return false;
    }
    // show(mp,size);
  }
  return true;
}

int main(void) {
  cin >> n;
  a.resize(n);
  rep(i, n) scanf("%d", &a[i]);
  int l = 0, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (check(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define all(a) (a).begin(), (a).end()
#define debug(x) cerr << (#x) << ": " << (x) << endl

int n;
vector<int> a;

void show(map<int, int> mp, int size) {
  rep1(i, size) { cout << char('a' + mp[i]); }
  cout << endl;
}

int check(int m) {
  // cout << char('a'+m-1) << "まで" << endl;
  map<int, int> mp;
  rep1(i, n - 1) {
    if (a[i - 1] < a[i])
      continue;
    if (m == 1)
      return false;
    int pos = a[i];
    mp.erase(mp.upper_bound(pos), mp.end());
    while (true) {
      if (mp[pos] < m - 1) {
        mp[pos]++;
        break;
      }
      mp.erase(--mp.end());
      pos--;
      if (pos == 0)
        return false;
    }
    // show(mp,size);
  }
  return true;
}

int main(void) {
  cin >> n;
  a.resize(n);
  rep(i, n) scanf("%d", &a[i]);
  int l = 0, r = n;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (check(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>

#define M 400010
#define PI pair<int, int>
#define f first
#define s second
using namespace std;
typedef long long ll;
int n, ans;

int X[M];

bool check(int x) {
  int t = 1;
  vector<PI> V(0);
  for (int i = 2; i <= n; i++)
    if (X[i] <= X[i - 1]) {
      while (!V.empty() && V.back().f > X[i])
        V.pop_back();
      if (V.empty() || V.back().f != X[i])
        V.push_back(PI(X[i], 0));
      V.back().s++;
      while (!V.empty() && V.back().s == x) {
        PI tmp = V.back();
        V.pop_back();
        if (V.empty() || V.back().f != tmp.f - 1)
          V.push_back(PI(tmp.f - 1, 0));
        V.back().s++;
        if (V.back().f == 0)
          return 0;
      }
    }
  return 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &X[i]);
  int l = 1, r = n;
  int flag = 1;
  for (int i = 2; i <= n; i++)
    if (X[i] <= X[i - 1]) {
      flag = 0;
      break;
    }
  if (flag) {
    puts("1");
    return 0;
  }
  for (; l <= r;) {
    int md = (l + r) / 2;
    if (check(md))
      ans = md, r = md - 1;
    else
      l = md + 1;
  }
  printf("%d\n", ans);
  return 0;
}

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

#define M 400010
#define PI pair<int, int>
#define f first
#define s second
using namespace std;
typedef long long ll;
int n, ans;

int X[M];

bool check(int x) {
  int t = 1;
  vector<PI> V(0);
  for (int i = 2; i <= n; i++)
    if (X[i] <= X[i - 1]) {
      while (!V.empty() && V.back().f > X[i])
        V.pop_back();
      if (V.empty() || V.back().f != X[i])
        V.push_back(PI(X[i], 0));
      V.back().s++;
      while (!V.empty() && V.back().s == x) {
        PI tmp = V.back();
        V.pop_back();
        if (V.empty() || V.back().f != tmp.f - 1)
          V.push_back(PI(tmp.f - 1, 0));
        V.back().s++;
        if (V.back().f == 0)
          return 0;
      }
    }
  return 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &X[i]);
  int l = 2, r = n;
  int flag = 1;
  for (int i = 2; i <= n; i++)
    if (X[i] <= X[i - 1]) {
      flag = 0;
      break;
    }
  if (flag) {
    puts("1");
    return 0;
  }
  for (; l <= r;) {
    int md = (l + r) / 2;
    if (check(md))
      ans = md, r = md - 1;
    else
      l = md + 1;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int a[131072];
int wz[131072], val[131072];
int num = 0;
int mid;
bool inc(int wz1) {
  if (wz1 <= 0) {
    return false;
  }
  if (mid == 1) {
    return false;
  }
  while (num) {
    if (wz[num - 1] <= wz1) {
      break;
    }
    num--;
  }
  if (!num) {
    wz[num] = wz1;
    val[num] = 0;
    num++;
  }
  if (wz[num - 1] < wz1) {
    wz[num] = wz1;
    val[num] = 0;
    num++;
  }
  val[num - 1]++;
  if (val[num - 1] == mid) {
    return inc(wz1 - 1);
  }
  return true;
}
int len = 0;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 1, r = n;
  while (l < r) {
    mid = ((l + r) >> 1);
    num = 0;
    len = 0;
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (a[i] > len) {
        len = a[i];
      } else {
        len = a[i];
        if (!inc(len)) {
          ok = false;
          break;
        }
      }
    }
    if (ok) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
  return 0;
}

Fixed submission:
#include <stdio.h>
int a[262144];
int wz[262144], val[262144];
int num = 0;
int mid;
bool inc(int wz1) {
  if (wz1 <= 0) {
    return false;
  }
  if (mid == 1) {
    return false;
  }
  while (num) {
    if (wz[num - 1] <= wz1) {
      break;
    }
    num--;
  }
  if (!num) {
    wz[num] = wz1;
    val[num] = 0;
    num++;
  }
  if (wz[num - 1] < wz1) {
    wz[num] = wz1;
    val[num] = 0;
    num++;
  }
  val[num - 1]++;
  if (val[num - 1] == mid) {
    return inc(wz1 - 1);
  }
  return true;
}
int len = 0;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 1, r = n;
  while (l < r) {
    mid = ((l + r) >> 1);
    num = 0;
    len = 0;
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (a[i] > len) {
        len = a[i];
      } else {
        len = a[i];
        if (!inc(len)) {
          ok = false;
          break;
        }
      }
    }
    if (ok) {
      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;
typedef long long ll;

const int MX = 200005;

int A[MX];

vector<pair<int, int>> st;
int lt[MX];
int a[MX];

int M;

void inc(int i, int t) {
  while (true) {
    int j = (int)st.size() - 1;
    while (st[j].first > i)
      j--;
    if (lt[i] < st[j].second) {
      lt[i] = t;
      a[i] = 0;
    }

    // cout << st[j].first << " " << st[j].second << " " << i << endl;

    a[i]++;
    if (i > 0 && a[i] == M) {
      a[i] = 0;
      i--;
    } else {
      break;
    }
  }
}

int main() {
  int n;
  ignore = scanf("%d", &n);

  for (int i = 1; i <= n; i++) {
    ignore = scanf("%d", A + i);
  }

  int L = 1, R = n - 1;
  int B = n;

  // int L = 2, R = 2; /////
  // int B = 0; //////

  while (L <= R) {
    M = (L + R) / 2;

    memset(lt, 0, sizeof lt);
    memset(a, 0, sizeof a);
    st.clear();

    bool ok = true;
    st.push_back({1, 0});
    for (int i = 1; i <= n; i++) {
      while (A[i] + 1 <= st.back().first)
        st.pop_back();
      st.push_back({A[i] + 1, i});

      if (i > 1 && A[i] <= A[i - 1]) {
        inc(A[i], i);
      }

      if (a[0] > 0) {
        ok = false;
        break;
      }
    }

    if (ok) {
      B = M;
      R = M - 1;
    } else {
      L = M + 1;
    }
  }

  printf("%d\n", B);
}


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

const int MX = 200005;

int A[MX];

vector<pair<int, int>> st;
int lt[MX];
int a[MX];

int M;

void inc(int i, int t) {
  while (true) {
    int j = (int)st.size() - 1;
    while (st[j].first > i)
      j--;
    if (lt[i] < st[j].second) {
      lt[i] = t;
      a[i] = 0;
    }

    // cout << st[j].first << " " << st[j].second << " " << i << endl;

    a[i]++;
    if (i > 0 && a[i] == M) {
      a[i] = 0;
      i--;
    } else {
      break;
    }
  }
}

int main() {
  int n;
  ignore = scanf("%d", &n);

  for (int i = 1; i <= n; i++) {
    ignore = scanf("%d", A + i);
    if (A[i] > 200000)
      A[i] = 200000;
  }

  int L = 1, R = n - 1;
  int B = n;

  // int L = 2, R = 2; /////
  // int B = 0; //////

  while (L <= R) {
    M = (L + R) / 2;

    memset(lt, 0, sizeof lt);
    memset(a, 0, sizeof a);
    st.clear();

    bool ok = true;
    st.push_back({1, 0});
    for (int i = 1; i <= n; i++) {
      while (A[i] + 1 <= st.back().first)
        st.pop_back();
      st.push_back({A[i] + 1, i});

      if (i > 1 && A[i] <= A[i - 1]) {
        inc(A[i], i);
      }

      if (a[0] > 0) {
        ok = false;
        break;
      }
    }

    if (ok) {
      B = M;
      R = M - 1;
    } else {
      L = M + 1;
    }
  }

  printf("%d\n", B);
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) cin >> A[i];
  vi Adec;
  int M = 0;
  rep(i, N - 1) {
    if (A[i] >= A[i + 1]) {
      Adec.push_back(A[i + 1]);
      M++;
    }
  }
  int L = 0;
  int R = InfL * 2;
  while (1) {
    int anstmp = (L + R) / 2;
    if (L == anstmp)
      break;
    map<int, int> Stmp;
    bool can = true;
    rep(i, M) {
      int Atmp = Adec[i];
      for (auto it = Stmp.upper_bound(Atmp); it != Stmp.end();)
        Stmp.erase(it++);
      while (1) {
        Stmp[Atmp]++;
        if (Stmp[Atmp] >= anstmp) {
          Stmp[Atmp] = 0;
          if (Atmp == 1) {
            can = false;
            break;
          }
          Atmp--;
        } else
          break;
      }
    }
    if (can)
      R = anstmp;
    else
      L = anstmp;
  }
  int ans = R;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define InfL 1000000000
#define InfLL 1000000000000000000LL
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n - 1); (i) >= (0); (i)--)
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<db> vd;

int main() {
  int N;
  cin >> N;
  vi A(N);
  rep(i, N) cin >> A[i];
  vi Adec;
  int M = 0;
  rep(i, N - 1) {
    if (A[i] >= A[i + 1]) {
      Adec.push_back(A[i + 1]);
      M++;
    }
  }
  if (M == 0) {
    cout << "1" << endl;
    return 0;
  }
  int L = 1;
  int R = InfL * 2;
  while (1) {
    int anstmp = (L + R) / 2;
    if (L == anstmp)
      break;
    map<int, int> Stmp;
    bool can = true;
    rep(i, M) {
      int Atmp = Adec[i];
      for (auto it = Stmp.upper_bound(Atmp); it != Stmp.end();)
        Stmp.erase(it++);
      while (1) {
        Stmp[Atmp]++;
        if (Stmp[Atmp] >= anstmp) {
          Stmp[Atmp] = 0;
          if (Atmp == 1) {
            can = false;
            break;
          }
          Atmp--;
        } else
          break;
      }
    }
    if (can)
      R = anstmp;
    else
      L = anstmp;
  }
  int ans = R;
  cout << ans << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>
using namespace std;

const int N = 100500;

int L[N];

int n;

struct item {
  bool zero;
  int x;
};

bool can(int al) {
  vector<item> st = {{true, L[0]}};
  for (int i = 1; i < n; i++) {
    if (L[i] > L[i - 1]) {
      st.emplace_back(item{true, L[i] - L[i - 1]});
    } else if (al == 1) {
      return false;
    } else {
      int cur = L[i - 1];
      while (cur > L[i]) {
        if (!st.back().zero) {
          st.pop_back();
          --cur;
        } else {
          int dec = min(st.back().x, cur - L[i]);
          st.back().x -= dec;
          cur -= dec;
          if (st.back().x == 0) {
            st.pop_back();
          } else {
            break;
          }
        }
      }
      for (int i = (int)st.size() - 1; i >= -1; i--) {
        if (i == -1) {
          return false;
        }
        if (st[i].zero) {
          st[i].x--;
          st.insert(st.begin() + i + 1, item{false, 1});
          if (st[i].x == 0) {
            st.erase(st.begin() + i);
          }
          break;
        } else if (st[i].x + 1 == al) {
          st[i].x = 0;
        } else {
          st[i].x++;
          break;
        }
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &L[i]);
  }
  int lo = 0, hi = n;
  while (lo + 1 < hi) {
    int mid = (lo + hi) / 2;
    if (can(mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  printf("%d\n", hi);
}


Fixed submission:
#include <cstdio>
#include <vector>
using namespace std;

const int N = 3e5 + 7;

int L[N];

int n;

struct item {
  bool zero;
  int x;
};

bool can(int al) {
  vector<item> st = {{true, L[0]}};
  for (int i = 1; i < n; i++) {
    if (L[i] > L[i - 1]) {
      st.emplace_back(item{true, L[i] - L[i - 1]});
    } else if (al == 1) {
      return false;
    } else {
      int cur = L[i - 1];
      while (cur > L[i]) {
        if (!st.back().zero) {
          st.pop_back();
          --cur;
        } else {
          int dec = min(st.back().x, cur - L[i]);
          st.back().x -= dec;
          cur -= dec;
          if (st.back().x == 0) {
            st.pop_back();
          } else {
            break;
          }
        }
      }
      for (int i = (int)st.size() - 1; i >= -1; i--) {
        if (i == -1) {
          return false;
        }
        if (st[i].zero) {
          st[i].x--;
          st.insert(st.begin() + i + 1, item{false, 1});
          if (st[i].x == 0) {
            st.erase(st.begin() + i);
          }
          break;
        } else if (st[i].x + 1 == al) {
          st[i].x = 0;
        } else {
          st[i].x++;
          break;
        }
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &L[i]);
  }
  int lo = 0, hi = n;
  while (lo + 1 < hi) {
    int mid = (lo + hi) / 2;
    if (can(mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  printf("%d\n", hi);
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

vec a(0);
bool ok(ll kind) {
  map<ll, ll> st;
  ll last = 0;
  st[0] = 0;
  // rep(i,N) st[a[i]] = 0;
  rep(i, N) {
    if (a[i] > 1e+6)
      continue;
    if (last > a[i]) {
      for (auto ite = st.rbegin(); ite->fi > a[i] && ite != st.rend(); ++ite)
        st.erase(ite->fi); // ite->se = 0;
      ++st[a[i]];
    } else if (last == a[i]) {
      ++st[a[i]];
    } else {
      st[a[i]] = 0;
    }
    ll see = a[i];
    while (st[see] >= kind && see != 0) {
      st.erase(see);
      --see;
      ++st[see];
    }
    if (see == 0) {
      st.clear();
      return false;
    }
    last = a[i];
  }
  st.clear();
  return true;
}
int main() {
  cin >> N;
  rep(i, N) cin >> A, a.push_back(A);
  ll lb = 0, ub = 2e+5;
  while (ub - lb > 1) {
    ll cen = (ub + lb) / 2;
    (ok(cen) ? ub : lb) = cen;
    // cout<<cen<<endl;
  }
  cout << ub << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

vec a(0);
bool ok(ll kind) {
  map<ll, ll> st;
  ll last = 0;
  st[0] = 0;
  // rep(i,N) st[a[i]] = 0;
  rep(i, N) {
    if (a[i] > 1e+6)
      continue;
    if (last > a[i]) {
      for (auto ite = st.rbegin(); ite->fi > a[i] && ite != st.rend(); ++ite)
        st.erase(ite->fi); // ite->se = 0;
      ++st[a[i]];
    } else if (last == a[i]) {
      ++st[a[i]];
    }
    ll see = a[i];
    while (st[see] >= kind && see != 0) {
      st.erase(see);
      --see;
      ++st[see];
    }
    if (see == 0) {
      st.clear();
      return false;
    }
    last = a[i];
  }
  st.clear();
  return true;
}
int main() {
  cin >> N;
  rep(i, N) cin >> A, a.push_back(A);
  ll lb = 0, ub = 2e+5;
  while (ub - lb > 1) {
    ll cen = (ub + lb) / 2;
    (ok(cen) ? ub : lb) = cen;
    // cout<<cen<<endl;
  }
  cout << ub << endl;
}
Problem p03202 (C++)

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

using namespace std;

int main() {
  int n, g, I;
  cin >> n;
  long long S[n], M = 5000000000, p, k, L = 0, R = 200002, t;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  while (R - L > 1) {
    t = (L + R) / 2;
    g = 1;
    set<long long> Se;
    set<long long>::iterator it;
    map<long long, long long> Ma;
    for (int i = 1; i < n; i++) {
      it = Se.lower_bound(M - 2000000000);
      while (it != Se.end() && M - (*it) >= S[i]) {
        Ma[M - *it] = 0;
        Se.erase(it);
        it = Se.lower_bound(M - 200000000);
      }
      if (S[i] <= S[i - 1]) {
        if (t == 1) {
          g = 0;
          break;
        }
        I = S[i] - 1;
        while (I >= 0 && Ma[I] == t - 1) {
          Se.erase(Se.find(M - I));
          Ma[I] = 0;
          I--;
        }
        if (I < 0) {
          g = 0;
          break;
        }
        if (Ma[I] == 0) {
          Se.insert(M - I);
        }
        Ma[I]++;
      }
    }
    if (g == 1) {
      R = t;
    } else {
      L = t;
    }
  }
  cout << R;
  return 0;
}


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

using namespace std;

int main() {
  int n, g, I;
  cin >> n;
  long long S[n], M = 5000000000, p, k, L = 0, R = 200002, t;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  while (R - L > 1) {
    t = (L + R) / 2;
    g = 1;
    set<long long> Se;
    set<long long>::iterator it;
    unordered_map<long long, long long> Ma;
    for (int i = 1; i < n; i++) {
      it = Se.lower_bound(M - 2000000000);
      while (it != Se.end() && M - (*it) >= S[i]) {
        Ma[M - *it] = 0;
        Se.erase(it);
        it = Se.lower_bound(M - 200000000);
      }
      if (S[i] <= S[i - 1]) {
        if (t == 1) {
          g = 0;
          break;
        }
        I = S[i] - 1;
        while (I >= 0 && Ma[I] == t - 1) {
          Se.erase(Se.find(M - I));
          Ma[I] = 0;
          I--;
        }
        if (I < 0) {
          g = 0;
          break;
        }
        if (Ma[I] == 0) {
          Se.insert(M - I);
        }
        Ma[I]++;
      }
    }
    if (g == 1) {
      R = t;
    } else {
      L = t;
    }
  }
  cout << R;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
typedef long long lint;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef unsigned long long ulint;
#define endl '\n'
#define fst first
#define sed second
#define pb push_back
#define rint register int
#define SZ(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define reveal(x) cerr << #x << " = " << (x) << endl
#define rep(it, f, e) for (rint it = (f); it <= (e); ++it)
#define per(it, f, e) for (rint it = (f); it >= (e); --it)
#define repe(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
const int MAXN = 2e5 + 20;
int a[MAXN], n;
map<int, int> mp;
inline bool check(int mid) {
  mp.clear();
  int tmp = 0;
  rep(i, 2, n) if (a[i - 1] >= a[i]) {
    auto it = mp.lower_bound(a[i]);
    mp.erase(it, mp.end());
    ++mp[tmp = a[i] - 1];
    while (tmp >= 0 && mp[tmp] >= mid) {
      mp.erase(tmp), ++mp[--tmp];
    }
    if (tmp < 0)
      return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  rep(i, 1, n) { cin >> a[i]; }
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << r + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
typedef long long lint;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef unsigned long long ulint;
#define endl '\n'
#define fst first
#define sed second
#define pb push_back
#define rint register int
#define SZ(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define reveal(x) cerr << #x << " = " << (x) << endl
#define rep(it, f, e) for (rint it = (f); it <= (e); ++it)
#define per(it, f, e) for (rint it = (f); it >= (e); --it)
#define repe(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
const int MAXN = 2e5 + 20;
int a[MAXN], n;
map<int, int> mp;
inline bool check(int mid) {
  mp.clear();
  int tmp = 0;
  if (mid == 1) {
    rep(i, 2, n) {
      if (a[i - 1] >= a[i])
        return false;
    }
    return true;
  }
  rep(i, 2, n) if (a[i - 1] >= a[i]) {
    auto it = mp.lower_bound(a[i]);
    mp.erase(it, mp.end());
    ++mp[tmp = a[i] - 1];
    while (tmp >= 0 && mp[tmp] >= mid) {
      mp.erase(tmp), ++mp[--tmp];
    }
    if (tmp < 0)
      return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  rep(i, 1, n) { cin >> a[i]; }
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << r + 1 << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define mp(a, b) make_pair(a, b)
using namespace std;
const int N = 1e5 + 50;
int n, a[N];

bool chk(int x) {
  if (x == 1) {
    rep(i, 2, n) if (a[i] <= a[i - 1]) return false;
    return true;
  }
  set<pair<int, int>> s;
  rep(j, 2, n) {
    if (a[j] <= a[j - 1]) {
      while (!s.empty() && (s.rbegin()->first >= a[j]))
        s.erase(*s.rbegin());
      dec(i, a[j] - 1, 0) {
        if (s.empty() || s.rbegin()->first != i) {
          s.insert(mp(i, 1));
          break;
        }
        if (s.rbegin()->second + 1 < x) {
          auto t = *s.rbegin();
          s.erase(*s.rbegin());
          ++t.second;
          s.insert(t);
          break;
        } else {
          s.erase(*s.rbegin());
        }
      }
      if (s.empty())
        return false;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", a + i);
  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (chk(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, s, t) for (int i = s; i <= (t); ++i)
#define dec(i, s, t) for (int i = s; i >= (t); --i)
#define mp(a, b) make_pair(a, b)
using namespace std;
const int N = 3e5 + 50;
int n, a[N];

bool chk(int x) {
  if (x == 1) {
    rep(i, 2, n) if (a[i] <= a[i - 1]) return false;
    return true;
  }
  set<pair<int, int>> s;
  rep(j, 2, n) {
    if (a[j] <= a[j - 1]) {
      while (!s.empty() && (s.rbegin()->first >= a[j]))
        s.erase(*s.rbegin());
      dec(i, a[j] - 1, 0) {
        if (s.empty() || s.rbegin()->first != i) {
          s.insert(mp(i, 1));
          break;
        }
        if (s.rbegin()->second + 1 < x) {
          auto t = *s.rbegin();
          s.erase(*s.rbegin());
          ++t.second;
          s.insert(t);
          break;
        } else {
          s.erase(*s.rbegin());
        }
      }
      if (s.empty())
        return false;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", a + i);
  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (chk(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r);
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
#pragma GCC optimize("trapv")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
const ll mod = 1e9 + 7;
int n, pos[200200], num[200200], sz;
vi a;
bool check(int x) {
  sz = 0;
  for (int i = 1; i < a.size(); i++) {
    if (a[i] > a[i - 1])
      continue;
    int j = a[i];
    while (j) {
      if (pos[sz - 1] > j) {
        --sz;
        continue;
      }
      if (!sz || pos[sz - 1] < j) {
        pos[sz] = j;
        num[sz] = 0;
        sz++;
      }
      if (++num[sz - 1] >= x) {
        sz--;
        j--;
      } else
        break;
    }
    if (!j)
      return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  ll ans = 0;
  for (int i = 1 << 17; i; i >>= 1)
    if (!check(ans + i))
      ans += i;
  cout << 1 + ans;
}


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("trapv")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
const ll mod = 1e9 + 7;
int n, pos[200200], num[200200], sz;
vi a;
bool check(int x) {
  sz = 0;
  for (int i = 1; i < a.size(); i++) {
    if (a[i] > a[i - 1])
      continue;
    int j = a[i];
    while (j) {
      if (pos[sz - 1] > j) {
        --sz;
        continue;
      }
      if (!sz || pos[sz - 1] < j) {
        pos[sz] = j;
        num[sz] = 0;
        sz++;
      }
      if (++num[sz - 1] >= x) {
        sz--;
        j--;
      } else
        break;
    }
    if (!j)
      return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  ll ans = 0;
  for (int i = 1 << 17; i; i >>= 1)
    if (!check(ans + i))
      ans += i;
  cout << 1 + ans;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 1e9 + 7;

int n, a[200005];

bool check(int m) {
  map<int, int> cur;
  for (int i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    map<int, int>::iterator cc;
    while ((cc = cur.upper_bound(a[i])) != cur.end())
      cur.erase(cc);
    cur[a[i]]++;
    int cp = a[i];
    while (cur[cp] >= m) {
      cur.erase(cp);
      cp--;
      cur[cp]++;
      if (cp == 0)
        return false;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d", &a[i]);
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 1e9 + 7;

int n, a[200005];

bool check(int m) {
  if (m == 1) {
    for (int i = 1; i < n; i++)
      if (a[i] <= a[i - 1])
        return false;
    return true;
  }
  map<int, int> cur;
  for (int i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    map<int, int>::iterator cc;
    while ((cc = cur.upper_bound(a[i])) != cur.end())
      cur.erase(cc);
    cur[a[i]]++;
    int cp = a[i];
    while (cur[cp] >= m) {
      cur.erase(cp);
      cp--;
      cur[cp]++;
      if (cp == 0)
        return false;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d", &a[i]);
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}