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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep2(i, m, n) for (int i = int(m); i < int(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll N;
  cin >> N;
  multiset<int> a;
  rep(i, N) {
    int x;
    cin >> x;
    a.insert(x);
  }

  int ans = 0;
  while (!a.empty()) {
    auto it = max_element(all(a));
    int x = *it;
    int k = 0;
    while (x) {
      k++;
      x /= 2;
    }
    int y = (1 << k) - *it;
    a.erase(it);

    it = a.find(y);
    if (it != a.end()) {
      a.erase(it);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep2(i, m, n) for (int i = int(m); i < int(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(a) a.begin(), a.end()
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ll N;
  cin >> N;
  multiset<int> a;
  rep(i, N) {
    int x;
    cin >> x;
    a.insert(x);
  }

  int ans = 0;
  while (!a.empty()) {
    auto it = a.end();
    it--;
    int x = *it;
    int k = 0;
    while (x) {
      k++;
      x /= 2;
    }
    int y = (1 << k) - *it;
    a.erase(it);

    it = a.find(y);
    if (it != a.end()) {
      a.erase(it);
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

ll smallest2(ll a) {
  ll rtn = 1;
  while (rtn <= a)
    rtn *= 2;
  return rtn;
}

int main() {
  int n;
  cin >> n;
  map<ll, int> cnt;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    cnt[a]++;
  }

  int ans = 0;
  for (map<ll, int>::reverse_iterator it = cnt.rbegin();; it++) {
    if (it->second == 0)
      continue;
    if (it == cnt.rend())
      break;
    ll s = it->first, t = smallest2(s) - s;

    if (s == t) {
      ll p = cnt[s] / 2;
      ans += p;
      cnt[s] -= p;
    } else {
      ll p = min(cnt[s], cnt[t]);
      ans += p;
      cnt[s] -= p;
      cnt[t] -= p;
    }
  }

  cout << ans << endl;
  return 0;
}

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

ll smallest2(ll a) {
  ll rtn = 1;
  while (rtn <= a)
    rtn *= 2;
  return rtn;
}

int main() {
  int n;
  cin >> n;
  map<ll, int> cnt;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    cnt[a]++;
  }

  int ans = 0;
  for (map<ll, int>::reverse_iterator it = cnt.rbegin();; it++) {
    if (&it == 0)
      continue;
    if (it == cnt.rend())
      break;
    ll s = it->first, t = smallest2(s) - s;

    if (s == t) {
      ll p = cnt[s] / 2;
      ans += p;
      cnt[s] -= p;
    } else {
      ll p = min(cnt[s], cnt[t]);
      ans += p;
      cnt[s] -= p;
      cnt[t] -= p;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03201 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[100001];
int ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].first;
  sort(a, a + n);
  for (int i = n; i--;) { // 好聪明啊
    if (a[i].second)
      continue;
    int t = 2 << __lg(a[i].first); // fantastic
    int k = upper_bound(a, a + i, make_pair(t - a[i].first, 0)) - a; // wonderful
    if (k > 0 && k <= i && a[i].first + a[k - 1].first == t) {
      ans++;
      a[k - 1].second = 1;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[200001];
int ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].first;
  sort(a, a + n);
  for (int i = n; i--;) { // 好聪明啊
    if (a[i].second)
      continue;
    int t = 2 << __lg(a[i].first); // fantastic
    int k = upper_bound(a, a + i, make_pair(t - a[i].first, 0)) - a; // wonderful
    if (k > 0 && k <= i && a[i].first + a[k - 1].first == t) {
      ans++;
      a[k - 1].second = 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 200005;

int n;
int arr[N];

bool ok(int x) {
  map<int, int> cnt;
  for (int i = 1; i < n; i++) {
    if (arr[i] <= arr[i - 1]) {
      int ce = arr[i];
      while (!cnt.empty() && (--cnt.end())->first > ce) {
        cnt.erase(--cnt.end());
      }
      cnt[ce]++;
      while (cnt[ce] >= x) {
        cnt.erase(ce--);
        cnt[ce]++;
        if (ce == 0)
          return 0;
      }
    }
  }
  return 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int l = 0, r = 1e9;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 200005;

int n;
int arr[N];

bool ok(int x) {
  map<int, int> cnt;
  for (int i = 1; i < n; i++) {
    if (arr[i] <= arr[i - 1]) {
      int ce = arr[i];
      while (!cnt.empty() && (--cnt.end())->first > ce) {
        cnt.erase(--cnt.end());
      }
      cnt[ce]++;
      while (cnt[ce] >= x) {
        cnt.erase(ce--);
        cnt[ce]++;
        if (ce == 0)
          return 0;
      }
    }
  }
  return 1;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  bool ww = 1;
  for (int i = 1; i < n; i++) {
    if (arr[i] <= arr[i - 1]) {
      ww = 0;
    }
  }
  if (ww) {
    cout << 1 << endl;
    return 0;
  }
  int l = 1, r = 1e9;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1);
const int M = 1e3 + 10;
const int N = 1e6 + 10;

int a[N];

void solve() {
  int n;
  scanf("%d", &n);

  fr(i, n) { scanf("%d", &a[i]); }

  vector<int> v;
  frab(i, 1, n) {
    if (a[i] <= a[i - 1]) {
      v.pb(a[i]);
    }
  }
  // sort(v.begin(), v.end());
  // reverse(v.begin(), v.end());

  // for (auto x: v) cout << x << " ";cout << endl;

  ll tl = 0, tr = n + 2;

  while (tr - tl > 1) {
    ll tm = (tl + tr) / 2;
    auto u = v;
    map<int, int, std::greater<int>> m;

    fr(i, u.size()) {
      int x = u[i];
      while (m.size() && m.begin()->first > x)
        m.erase(m.begin());
      m[x]++;
      while (m.count(x) && m[x] >= tm && x > 1) {
        m[x - 1] += m[x] / tm;
        m[x] %= tm;
        x--;
      }
    }

    if (u.size())
      m[1] = max(1, m[1]);

    if (m[1] >= tm)
      tl = tm;
    else
      tr = tm;
  }
  cout << tr << "\n";
}

int main() {
  // freopen("a.in", "r", stdin);
  // ios_base::sync_with_stdio(false);
  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1);
const int M = 1e3 + 10;
const int N = 1e6 + 10;

int a[N];

void solve() {
  int n;
  scanf("%d", &n);

  fr(i, n) { scanf("%d", &a[i]); }

  vector<int> v;
  frab(i, 1, n) {
    if (a[i] <= a[i - 1]) {
      v.pb(a[i]);
    }
  }
  // sort(v.begin(), v.end());
  // reverse(v.begin(), v.end());

  // for (auto x: v) cout << x << " ";cout << endl;

  ll tl = 0, tr = n + 2;

  while (tr - tl > 1) {
    ll tm = (tl + tr) / 2;
    auto u = v;
    map<int, int, std::greater<int>> m;

    fr(i, u.size()) {
      int x = u[i];
      while (m.size() && m.begin()->first > x)
        m.erase(m.begin());
      m[x]++;
      if (tm > 1) {
        while (m.count(x) && m[x] >= tm && x > 1) {
          m[x - 1] += m[x] / tm;
          m[x] %= tm;
          x--;
        }
      }
    }

    if (u.size())
      m[1] = max(1, m[1]);

    if (m[1] >= tm)
      tl = tm;
    else
      tr = tm;
  }
  cout << tr << "\n";
}

int main() {
  // freopen("a.in", "r", stdin);
  // ios_base::sync_with_stdio(false);
  solve();

  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
using namespace std;
typedef long long ll;
inline int read() {
  int n = 0, f = 1;
  char c;
  for (c = getchar(); c != '-' && (c < '0' || c > '9'); c = getchar())
    ;
  if (c == '-')
    f = -1, c = getchar();
  for (; c >= '0' && c <= '9'; c = getchar())
    n = (n << 3) + (n << 1) + (c & 15);
  return n * f;
}
const int maxn = 2e5 + 5;
int i, j, k, s, n, len, a[maxn];
map<int, int> p;
map<int, int>::iterator it;
int main() {
  n = read();
  fo(i, 1, n) a[i] = read();
  int l = 1, r = n;
  for (; l < r;) {
    int m = (l + r) >> 1;
    p.clear();
    len = 0;
    for (i = 1; i <= n; i++) {
      int x = a[i];
      if (x > len) {
        len = x;
        continue;
      }
      for (;;) {
        it = p.end();
        if (it != p.begin())
          it--;
        else
          break;
        if (it->first > x)
          p.erase(it);
        else
          break;
      }
      p[x]++, len = x;
      for (; x && p[x] == m;)
        p.erase(p.find(x)), p[--x]++;
      if (x <= 0)
        break;
    }
    if (i > n)
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
using namespace std;
typedef long long ll;
inline int read() {
  int n = 0, f = 1;
  char c;
  for (c = getchar(); c != '-' && (c < '0' || c > '9'); c = getchar())
    ;
  if (c == '-')
    f = -1, c = getchar();
  for (; c >= '0' && c <= '9'; c = getchar())
    n = (n << 3) + (n << 1) + (c & 15);
  return n * f;
}
const int maxn = 2e5 + 5;
int i, j, k, s, n, len, a[maxn];
map<int, int> p;
map<int, int>::iterator it;
int main() {
  n = read();
  fo(i, 1, n) a[i] = read();
  int l = 1, r = n;
  for (; l < r;) {
    int m = (l + r) >> 1;
    p.clear();
    len = 0;
    for (i = 1; i <= n; i++) {
      int x = a[i];
      if (x > len) {
        len = x;
        continue;
      }
      for (;;) {
        it = p.end();
        if (it != p.begin())
          it--;
        else
          break;
        if (it->first > x)
          p.erase(it);
        else
          break;
      }
      if (m == 1)
        break;
      p[x]++, len = x;
      for (; x && p[x] == m;)
        p.erase(p.find(x)), p[--x]++;
      if (x <= 0)
        break;
    }
    if (i > n)
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define N 200010
using namespace std;
typedef int ll;
ll p[N];
map<ll, ll> mp;
ll a;
bool check(ll k) {
  mp.clear();
  for (ll i = 2; i <= a; i++)
    if (p[i] > p[i - 1])
      continue;
    else {

      while (mp.size()) {
        ll u = mp.rbegin()->first;
        if (u > p[i])
          mp.erase(u);
        else
          break;
      }

      ll j = p[i];
      while (j && mp[j] + 1 == k) {

        mp.erase(j);
        j--;
      }
      if (j == 0)
        return false;
      mp[j]++;
    }
  return true;
}
int main() {

  cin >> a;
  for (ll i = 1; i <= a; i++)
    cin >> p[i];

  ll l = 1, r = a, mid;

  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }

  cout << l << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 200010
using namespace std;
typedef int ll;
ll p[N];
map<ll, ll> mp;
ll a;
bool check(ll k) {
  mp.clear();
  for (ll i = 2; i <= a; i++)
    if (p[i] > p[i - 1])
      continue;
    else {
      if (k == 1)
        return false;

      while (mp.size()) {
        ll u = mp.rbegin()->first;
        if (u > p[i])
          mp.erase(u);
        else
          break;
      }

      ll j = p[i];
      while (j && mp[j] + 1 == k) {

        mp.erase(j);
        j--;
      }
      if (j == 0)
        return false;
      mp[j]++;
    }
  return true;
}
int main() {

  cin >> a;
  for (ll i = 1; i <= a; i++)
    cin >> p[i];

  ll l = 1, r = a, mid;

  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }

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

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

// #define int long long
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}
unordered_map<int, int> mp;
set<int> st;
void clr() {
  for (auto it : st)
    mp[it] = 0;
  st.clear();
}

bool check(int sz, vector<int> &a) {
  for (int i = 1; i < a.size(); ++i) {
    int c = a[i];
    if (sz != 1 && c >= 5e5)
      continue;
    if (a[i - 1] >= a[i]) {
      while (st.size() > 0 && *st.rbegin() > c) {
        mp[*st.rbegin()] = 0;
        st.erase(*st.rbegin());
      }
      while (c >= 1 && mp[c] + 1 == sz) {
        mp[c] = 0;
        --c;
      }
      if (c == 0) {
        clr();
        return false;
      }
      mp[c]++;
      st.insert(c);
    } else {
      continue;
    }
  }
  clr();
  return true;
}

signed main() {
  accell();
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  bool ok = true;
  for (int i = 0; i + 1 < n; ++i)
    ok &= (a[i] < a[i + 1]);
  if (ok) {
    cout << 1;
    return 0;
  }
  int l = 0;
  int r = n + 1;
  while (r - l > 1) {
    int m = l + r >> 1;
    if (!check(m, a))
      l = m;
    else
      r = m;
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

// #define int long long
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void accell() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}
unordered_map<int, int> mp;
set<int> st;
void clr() {
  for (auto it : st)
    mp[it] = 0;
  st.clear();
}

bool check(int sz, vector<int> &a) {
  for (int i = 1; i < a.size(); ++i) {
    int c = a[i];
    if (c >= 5e5)
      continue;
    if (a[i - 1] >= a[i]) {
      while (st.size() > 0 && *st.rbegin() > c) {
        mp[*st.rbegin()] = 0;
        st.erase(*st.rbegin());
      }
      while (c >= 1 && mp[c] + 1 == sz) {
        mp[c] = 0;
        --c;
      }
      if (c == 0) {
        clr();
        return false;
      }
      mp[c]++;
      st.insert(c);
    } else {
      continue;
    }
  }
  clr();
  return true;
}

signed main() {
  accell();
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  bool ok = true;
  for (int i = 0; i + 1 < n; ++i)
    ok &= (a[i] < a[i + 1]);
  if (ok) {
    cout << 1;
    return 0;
  }
  int l = 0;
  int r = n + 1;
  while (r - l > 1) {
    int m = l + r >> 1;
    if (!check(m, a))
      l = m;
    else
      r = m;
  }
  cout << r << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n;
int a[200001];

bool check(int x) {
  map<int, int>
      str; //(桁の位置(0-indexed), 桁の値(0～x-1)). (pos, 0)以外を記録.
  int i;

  for (i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (!str.empty() && str.rbegin()->first >= a[i])
      str.erase(str.rbegin()->first);
    str[a[i] - 1]++;

    int cur = a[i] - 1;
    while (str[cur] >= x) {
      str.erase(cur);
      cur--;
      str[cur]++;
      if (cur < 0) {
        return false;
      }
    }
  }
  return true;
}

int main() {
  int i;

  cin >> n;
  a[0] = 0;
  rep(i, n) cin >> a[i + 1];
  n++;

  int st = 0, ed = n, mid; // xxxooo, (st, ed]
  while (ed - st >= 2) {
    mid = (st + ed) / 2;
    if (check(mid))
      ed = mid;
    else
      st = mid;
  }
  cout << ed << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n;
int a[200001];

bool check(int x) {
  map<int, int>
      str; //(桁の位置(0-indexed), 桁の値(0～x-1)). (pos, 0)以外を記録.
  int i;

  for (i = 1; i < n; i++) {
    if (a[i] > a[i - 1])
      continue;
    while (!str.empty() && str.rbegin()->first >= a[i])
      str.erase(str.rbegin()->first);
    str[a[i] - 1]++;

    int cur = a[i] - 1;
    while (str[cur] >= x) {
      str.erase(cur);
      cur--;
      str[cur]++;
      if (cur < 0) {
        return false;
      }
    }
  }
  return true;
}

int main() {
  int i;

  cin >> n;
  a[0] = 0;
  rep(i, n) cin >> a[i + 1];
  n++;

  // check(1)を呼び出すと,桁上がりの処理がO(A_i)回行われる可能性があるので、答え=1かの判定は先にやる
  bool ok1 = true;
  for (i = 1; i < n; i++)
    if (a[i] <= a[i - 1])
      ok1 = false;
  if (ok1) {
    cout << 1 << endl;
    return 0;
  }

  // 2分探索
  int st = 1, ed = n, mid; // xxxooo, (st, ed]
  while (ed - st >= 2) {
    mid = (st + ed) / 2;
    if (check(mid))
      ed = mid;
    else
      st = mid;
  }
  cout << ed << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using PP = pair<long, long>;
const int INF = 1e9;
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}

int n;
int a[200000];

bool ok(int kind) {
  map<int, int> mp;
  for (int i = 1; i < n; ++i) {
    if (a[i - 1] < a[i])
      continue;

    int idx = a[i];
    while (idx > 0 && mp[idx] == kind - 1) {
      mp[idx] = 0;
      --idx;
    }
    if (idx == 0)
      return false;
    ++mp[idx];

    mp.erase(mp.upper_bound(idx), mp.end());
  }
  return true;
}

int sub() {
  int lo = 0;
  int hi = 200001;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (ok(mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  return hi;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  cout << sub() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using PP = pair<long, long>;
const int INF = 1e9;
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}

int n;
int a[200000];

bool ok(int kind) {
  map<int, int> mp;
  for (int i = 1; i < n; ++i) {
    if (a[i - 1] < a[i])
      continue;
    if (kind == 1)
      return false;

    int idx = a[i];
    while (idx > 0 && mp[idx] == kind - 1) {
      mp[idx] = 0;
      --idx;
    }
    if (idx == 0)
      return false;
    ++mp[idx];

    mp.erase(mp.upper_bound(idx), mp.end());
  }
  return true;
}

int sub() {
  int lo = 0;
  int hi = 200001;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (ok(mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  return hi;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  cout << sub() << endl;
}
Problem p03202 (C++)

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

const int N = 200005;
int n, a[N];

bool check(int mid) {
  map<int, int> b;
  for (int i = 2; i <= n; ++i) {
    while (!b.empty() && b.rbegin()->first >= a[i])
      b.erase(--b.end());
    if (a[i] <= a[i - 1]) {
      ++b[a[i] - 1];
      for (int j = a[i] - 1; b.count(j) && b[j] == mid; --j) {
        b.erase(j);
        if (j == 0)
          return false;
        ++b[j - 1];
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    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>
using namespace std;

const int N = 200005;
int n, a[N];

bool check(int mid) {
  map<int, int> b;
  for (int i = 2; i <= n; ++i) {
    while (!b.empty() && b.rbegin()->first >= a[i])
      b.erase(--b.end());
    if (a[i] <= a[i - 1]) {
      ++b[a[i] - 1];
      for (int j = a[i] - 1; b.count(j) && b[j] == mid; --j) {
        b.erase(j);
        if (j == 0)
          return false;
        ++b[j - 1];
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  bool flag = true;
  for (int i = 2; i <= n; ++i)
    flag &= a[i] > a[i - 1];
  if (flag)
    return puts("1"), 0;
  int l = 2, 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;
}
Problem p03202 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

bool add(map<int, int> &a, int d, int b) {
  for (int j = d; j >= 0; j--) {
    a[j]++;
    if (a[j] == b) {
      if (j == 0)
        return false;
      a[j] = 0;
    } else {
      break;
    }
  }
  return true;
}

int n;
int a[200010];

bool ok(int X) {
  map<int, int> crt;
  repl(i, 1, n) {
    if (a[i - 1] >= a[i]) {
      while (crt.lower_bound(a[i]) != crt.end()) {
        crt.erase(crt.lower_bound(a[i]));
      }
      if (!add(crt, a[i] - 1, X))
        return false;
    }
  }

  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i, n) { cin >> a[i]; }

  int lb = 0, ub = n + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (ok(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;

  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

bool add(map<int, int> &a, int d, int b) {
  for (int j = d; j >= 0; j--) {
    a[j]++;
    if (a[j] == b) {
      if (j == 0)
        return false;
      a[j] = 0;
    } else {
      break;
    }
  }
  return true;
}

int n;
int a[200010];

bool ok(int X) {
  if (X == 1) {
    rep(i, n - 1) {
      if (a[i] >= a[i + 1])
        return false;
    }
    return true;
  }
  map<int, int> crt;
  repl(i, 1, n) {
    if (a[i - 1] >= a[i]) {
      while (crt.lower_bound(a[i]) != crt.end()) {
        crt.erase(crt.lower_bound(a[i]));
      }
      if (!add(crt, a[i] - 1, X))
        return false;
    }
  }

  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i, n) { cin >> a[i]; }

  int lb = 0, ub = n + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (ok(mid))
      ub = mid;
    else
      lb = mid;
  }
  cout << ub << endl;

  return 0;
}
Problem p03202 (C++)

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

int main() {
  int N;
  cin >> N;

  vector<ll> A(N);
  for (auto &e : A) {
    cin >> e;
  }

  int l = 0, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;

    map<int, int> mp;
    mp[A[0]] = 0;
    bool ok = true;
    for (int i = 1; i < N; i++) {
      if (A[i - 1] < A[i]) {
        auto it = --mp.end();
        if (it->second == 0) {
          mp.erase(it);
        }
        mp[A[i]] = 0;
      } else {
        auto it = mp.lower_bound(A[i]);
        if (it->second == mid - 1) {
          while (it != mp.begin() && it->second == mid - 1) {
            --it;
          }
          if (it == mp.begin() && it->second == mid - 1) {
            ok = false;
            break;
          }
          if (it->first != 1 && mp.find(it->first - 1) == mp.end()) {
            mp[it->first - 1] = it->second;
          }
          it->second++;

          auto nit = --mp.end();
          while (it != nit) {
            mp.erase(nit);
            nit = --mp.end();
          }
          assert(it->first < A[i]);
          mp[A[i]] = 0;
        } else {
          if (A[i] != 1 && mp.lower_bound(A[i] - 1) == it) {
            mp[A[i] - 1] = it->second;
          }
          mp[A[i]] = it->second + 1;

          mp.erase(it, mp.end());
        }
      }
    }
    if (ok) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;

  return 0;
}

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

int main() {
  int N;
  cin >> N;

  vector<ll> A(N);
  for (auto &e : A) {
    cin >> e;
  }

  int l = 0, r = N;
  while (r - l > 1) {
    int mid = (l + r) / 2;

    map<int, int> mp;
    mp[A[0]] = 0;
    bool ok = true;
    for (int i = 1; i < N; i++) {
      if (A[i - 1] < A[i]) {
        auto it = --mp.end();
        if (it->second == 0) {
          mp.erase(it);
        }
        mp[A[i]] = 0;
      } else {
        auto it = mp.lower_bound(A[i]);
        if (it->second == mid - 1) {
          while (it != mp.begin() && it->second == mid - 1) {
            --it;
          }
          if (it == mp.begin() && it->second == mid - 1) {
            ok = false;
            break;
          }
          if (it->first != 1 && mp.find(it->first - 1) == mp.end()) {
            mp[it->first - 1] = it->second;
          }
          it->second++;

          auto nit = --mp.end();
          while (it != nit) {
            mp.erase(nit);
            nit = --mp.end();
          }
          assert(it->first < A[i]);
          mp[A[i]] = 0;
        } else {
          if (A[i] != 1 && mp.lower_bound(A[i] - 1) == it) {
            mp[A[i] - 1] = it->second;
          }
          mp[A[i]] = it->second + 1;

          mp.erase(mp.upper_bound(A[i]), mp.end());
        }
      }
    }
    if (ok) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;

  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[200005];
map<ll, ll> p;
bool check(ll x) {
  p.clear();
  for (int i = 1; i < n; i++) {
    ll b = a[i - 1], c = a[i];
    if (b >= c) {
      p.erase(p.upper_bound(c), p.end());
      while (1) {
        if (c == 0)
          return false;
        if (++p[c] < x)
          break;
        p.erase(c);
        c--;
      }
    }
  }
  return true;
}
int main(void) {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", a + i);
  ll l = 0, r = n;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m;
  }
  printf("%lld\n", r);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[200005];
map<ll, ll> p;
bool check(ll x) {
  p.clear();
  for (int i = 1; i < n; i++) {
    ll b = a[i - 1], c = a[i];
    if (b >= c) {
      if (x == 1)
        return false;
      p.erase(p.upper_bound(c), p.end());
      while (1) {
        if (c == 0)
          return false;
        if (++p[c] < x)
          break;
        p.erase(c);
        c--;
      }
    }
  }
  return true;
}
int main(void) {
  scanf("%lld", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", a + i);
  ll l = 0, r = n;
  while (r - l > 1) {
    ll m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m;
  }
  printf("%lld\n", r);
}
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++)
#define brep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define brep1(i, n) for (int i = n - 1; i >= 1; i--)

vector<int> a;
int n;

bool check(int x) { // x文字以内で要件を満たせるかどうか
  map<int, int> mp;
  rep1(i, a.size()) {
    if (a[i - 1] < a[i])
      continue; // 後ろに文字追加してるだけなのでスキップ
    mp[a[i]]++; // a[i]文字目の番号を+1
    mp.erase(mp.upper_bound(a[i]), mp.end()); // a[i]より後ろはリセット
    brep1(j, a[i] + 1) {
      if (mp[j] >= x) {
        if (j == 1)
          return false; // 最初の文字を越えて繰り上がるようなら失敗
        mp[j] = 0;
        mp[j - 1]++; // 繰り上がり
      } else
        break; // 繰り上がり処理の終了
    }
  }
  return true; // 最後までいったら成功
}

int main(void) {
  cin >> n;
  rep(i, n) {
    int num;
    cin >> num;
    a.emplace_back(num);
  }
  int min = 0;
  int max = n;
  while (max - min > 1) { // min<x<=maxでxを二部探索
    int mid = (max + min) / 2;
    if (check(mid))
      max = mid;
    else
      min = mid;
  }
  cout << max << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define brep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define brep1(i, n) for (int i = n - 1; i >= 1; i--)

vector<int> a;
int n;

bool check(int x) { // x文字以内で要件を満たせるかどうか
  map<int, int> mp;
  rep1(i, a.size()) {
    if (a[i - 1] < a[i])
      continue; // 後ろに文字追加してるだけなのでスキップ
    if (x == 1)
      return false;
    mp[a[i]]++;                               // a[i]文字目の番号を+1
    mp.erase(mp.upper_bound(a[i]), mp.end()); // a[i]より後ろはリセット
    brep1(j, a[i] + 1) {
      if (mp[j] >= x) {
        if (j == 1)
          return false; // 最初の文字を越えて繰り上がるようなら失敗
        mp[j] = 0;
        mp[j - 1]++; // 繰り上がり
      } else
        break; // 繰り上がり処理の終了
    }
  }
  return true; // 最後までいったら成功
}

int main(void) {
  cin >> n;
  rep(i, n) {
    int num;
    cin >> num;
    a.emplace_back(num);
  }
  int min = 0;
  int max = n;
  while (max - min > 1) { // min<x<=maxでxを二部探索
    int mid = (max + min) / 2;
    if (check(mid))
      max = mid;
    else
      min = mid;
  }
  cout << max << endl;
}
Problem p03202 (C++)

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

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fst first
#define scd second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, a[200005];
bool check(int mid) {
  map<int, int> mp;
  for (int i = 1; i <= n; ++i) {
    if (a[i] <= a[i - 1]) {
      while (mp.size() && mp.rbegin()->fst > a[i]) {
        map<int, int>::iterator it = mp.end();
        mp.erase(--it);
      }
      for (int j = a[i]; j >= 0; --j) {
        if (!j)
          return 0;
        mp[j]++;
        if (mp[j] == mid)
          mp[j] = 0;
        else
          break;
      }
    }
  }
  return 1;
}
int main() {
  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))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

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

#define pb push_back
#define mk make_pair
#define lob lower_bound
#define upb upper_bound
#define fst first
#define scd second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline ll readll() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, a[200005];
bool check(int mid) {
  map<int, int> mp;
  for (int i = 1; i <= n; ++i) {
    if (a[i] <= a[i - 1]) {
      if (mid == 1)
        return 0;
      while (mp.size() && mp.rbegin()->fst > a[i]) {
        map<int, int>::iterator it = mp.end();
        mp.erase(--it);
      }
      for (int j = a[i]; j >= 0; --j) {
        if (!j)
          return 0;
        mp[j]++;
        if (mp[j] == mid)
          mp[j] = 0;
        else
          break;
      }
    }
  }
  return 1;
}
int main() {
  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))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int n, a[N], l, r, t;
vector<int> p, q;
bool C(int k) {
  p.clear(), q.clear();
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1]) {
      if (k == 1)
        return false;
      while (!p.empty() && p[p.size() - 1] > a[i])
        p.pop_back(), q.pop_back();
      if (!p.empty() && p[p.size() - 1] == a[i])
        q[q.size() - 1]++;
      else
        p.push_back(a[i]), q.push_back(1);
      while (!p.empty() && q[q.size() - 1] == k) {
        t = p[p.size() - 1], p.pop_back(), q.pop_back();
        if (!p.size() && p[p.size() - 1] == t - 1)
          q[q.size() - 1]++;
        else
          p.push_back(t - 1), q.push_back(1);
      }
      if (p[0] == 0)
        return false;
    }
  return true;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  l = 1, r = n;
  while (l < r)
    if (C((l + r) / 2))
      r = (l + r) / 2;
    else
      l = (l + r) / 2 + 1;
  cout << l;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int n, a[N], l, r, t;
vector<int> p, q;
bool C(int k) {
  p.clear(), q.clear();
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1]) {
      if (k == 1)
        return false;
      while (!p.empty() && p[p.size() - 1] > a[i])
        p.pop_back(), q.pop_back();
      if (!p.empty() && p[p.size() - 1] == a[i])
        q[q.size() - 1]++;
      else
        p.push_back(a[i]), q.push_back(1);
      while (!p.empty() && q[q.size() - 1] == k) {
        t = p[p.size() - 1], p.pop_back(), q.pop_back();
        if (!p.empty() && p[p.size() - 1] == t - 1)
          q[q.size() - 1]++;
        else
          p.push_back(t - 1), q.push_back(1);
      }
      if (p[0] == 0)
        return false;
    }
  return true;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  l = 1, r = n;
  while (l < r)
    if (C((l + r) / 2))
      r = (l + r) / 2;
    else
      l = (l + r) / 2 + 1;
  cout << l;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr, __VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define SZ(x) ((int)(x).size())
#define mset(a, v) memset(a, v, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;

int n;
int a[200005];
map<int, int> d;

bool can(int b) {
  d.clear();
  fore(i, 1, n) {
    if (a[i] <= a[i - 1]) {
      while (!d.empty() && d.rbegin()->fst >= a[i])
        d.erase(d.rbegin()->fst);
      int k = a[i] - 1;
      while (k >= 0 && d[k] == b - 1)
        d[k] = 0, k--;
      if (k < 0)
        return false;
      d[k]++;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  fore(i, 0, n) scanf("%d", a + i);
  int s = 1, e = 200005;
  while (e - s > 1) {
    int m = (s + e) / 2;
    if (can(m - 1))
      e = m;
    else
      s = m;
  }
  printf("%d\n", s);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#ifdef DEMETRIO
#define deb(...) fprintf(stderr, __VA_ARGS__)
#define deb1(x) cerr << #x << " = " << x << endl
#else
#define deb(...) 0
#define deb1(x) 0
#endif
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define fore(i, a, b) for (int i = a, ThxDem = b; i < ThxDem; ++i)
#define SZ(x) ((int)(x).size())
#define mset(a, v) memset(a, v, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;

int n;
int a[200005];
map<int, int> d;

bool can(int b) {
  d.clear();
  fore(i, 1, n) {
    if (a[i] <= a[i - 1]) {
      if (b == 1)
        return false;
      while (!d.empty() && d.rbegin()->fst >= a[i])
        d.erase(d.rbegin()->fst);
      int k = a[i] - 1;
      while (k >= 0 && d[k] == b - 1)
        d[k] = 0, k--;
      if (k < 0)
        return false;
      d[k]++;
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  fore(i, 0, n) scanf("%d", a + i);
  int s = 1, e = 200005;
  while (e - s > 1) {
    int m = (s + e) / 2;
    if (can(m - 1))
      e = m;
    else
      s = m;
  }
  printf("%d\n", s);
  return 0;
}
Problem p03202 (C++)

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

typedef long long ll;
const ll INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 2e5 + 55;

int n;
ll a[maxN];

bool chk(ll x) {
  map<ll, ll> mp;
  for (int i = 2; i <= n; i++) {
    if (a[i - 1] >= a[i]) {
      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 == 0) {
        return false;
      }
      mp[j]++;
    }
  }

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

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

  int lo = 1, hi = n, ans = n;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (chk(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << '\n';

  return 0;
}

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

typedef long long ll;
const ll INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 2e5 + 55;

int n;
ll a[maxN];

bool chk(ll x) {
  map<ll, ll> mp;
  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 == 0) {
        return false;
      }
      mp[j]++;
    }
  }

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

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

  int lo = 1, hi = n, ans = n;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (chk(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }

  cout << ans << '\n';

  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
// user:halahen
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int i, j, k;
int n, a[maxn];
int l, r, mid, ans;
int read() {
  int tot = 0, fh = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9')) {
    tot = tot * 10 + c - '0';
    c = getchar();
  }
  return tot * fh;
}
map<int, int> mp;
int check(int col) {
  int i, j;
  mp.clear();
  for (i = 2; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      j = a[i - 1] + 1;
      while (-(mp.upper_bound(-j)->first) > a[i]) {
        j = -(mp.upper_bound(-j)->first);
        mp.erase(-j);
      }
      if (mp[-a[i]] == 0)
        mp[-a[i]] = 2;
      else
        mp[-a[i]]++;
      j = a[i];
      while (mp[-j] > col) {
        if (j == 1)
          return 0;
        mp[-j] = mp[-j] - col;
        if (mp[-j + 1] == 0)
          mp[-j + 1] = 1;
        mp[-j + 1] = mp[-j + 1] + 1;
        j--;
      }
    }
  }
  return 1;
}
int main() {
  n = read();
  for (i = 1; i <= n; i++) {
    a[i] = read();
  }
  l = 1;
  r = n;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid) == 1) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
// user:halahen
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int i, j, k;
int n, a[maxn];
int l, r, mid, ans;
int read() {
  int tot = 0, fh = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9')) {
    tot = tot * 10 + c - '0';
    c = getchar();
  }
  return tot * fh;
}
map<int, int> mp;
int check(int col) {
  int i, j;
  mp.clear();
  for (i = 2; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      if (col == 1)
        return 0;
      j = a[i - 1] + 1;
      while (-(mp.upper_bound(-j)->first) > a[i]) {
        j = -(mp.upper_bound(-j)->first);
        mp.erase(-j);
      }
      if (mp[-a[i]] == 0)
        mp[-a[i]] = 2;
      else
        mp[-a[i]]++;
      j = a[i];
      while (mp[-j] > col) {
        if (j == 1)
          return 0;
        mp[-j] = mp[-j] - col;
        if (mp[-j + 1] == 0)
          mp[-j + 1] = 1;
        mp[-j + 1] = mp[-j + 1] + 1;
        j--;
      }
    }
  }
  return 1;
}
int main() {
  n = read();
  for (i = 1; i <= n; i++) {
    a[i] = read();
  }
  l = 1;
  r = n;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid) == 1) {
      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 <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <functional>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200010;

int n;
int a[N];

bool pred(int x) {
  map<int, int> cur;
  for (int i = 2; i <= n; ++i) {
    if (a[i] > a[i - 1])
      continue;
    int pos = a[i] - 1;
    cur.erase(cur.lower_bound(a[i]), cur.end());
    while (pos && cur[pos] == x - 1)
      cur.erase(pos--);
    if (cur[pos] == x - 1)
      return false;
    ++cur[pos];
  }
  return true;
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif

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

#ifdef LBT
  LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <functional>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200010;

int n;
int a[N];

bool pred(int x) {
  map<int, int> cur;
  for (int i = 2; i <= n; ++i) {
    if (a[i] > a[i - 1])
      continue;
    int pos = a[i] - 1;
    cur.erase(cur.lower_bound(a[i]), cur.end());
    while (pos && cur[pos] == x - 1)
      cur.erase(pos--);
    if (cur[pos] == x - 1)
      return false;
    ++cur[pos];
  }
  return true;
}

int main() {
#ifdef LBT
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  bool flag = true;
  for (int i = 2; i <= n; ++i)
    flag &= a[i] > a[i - 1];
  if (flag) {
    puts("1");
    return 0;
  }
  int l = 2, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (pred(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);

#ifdef LBT
  LOG("Time: %dms\n", int((clock() - nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}