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

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

template <typename _tp> inline void read(_tp &x) {
  char ch = getchar(), ob = 0;
  x = 0;
  while (ch != '-' && !isdigit(ch))
    ch = getchar();
  if (ch == '-')
    ob = 1, ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  if (ob)
    x = -x;
}

const int N = 101000;
typedef pair<int, int> pii;
pii b[N];
int st[N];
int n, R, C, tp, tot;

inline int get(int x, int y) {
  if (x and x != R and y and y != C)
    return -1;
  if (y == 0)
    return x;
  if (x == R)
    return R + y;
  if (y == C)
    return R + C + (R - x);
  return R + C + R + (C - y);
}

int main() {
  //	freopen("in","r",stdin);
  read(R), read(C), read(n);
  for (int i = 1, x, y, t1, t2; i <= n; ++i) {
    read(x), read(y), t1 = get(x, y);
    read(x), read(y), t2 = get(x, y);
    if (t1 == -1 or t2 == -1)
      continue;
    b[++tot] = pii(t1, i);
    b[++tot] = pii(t2, i);
  }
  sort(b + 1, b + tot + 1);
  for (int i = 1; i <= tot; ++i)
    if (tp and st[tp] == b[i].second)
      --tp;
    else
      st[++tp] = b[i].second;
  puts(tp ? "NO" : "YES");
  return 0;
}

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

template <typename _tp> inline void read(_tp &x) {
  char ch = getchar(), ob = 0;
  x = 0;
  while (ch != '-' && !isdigit(ch))
    ch = getchar();
  if (ch == '-')
    ob = 1, ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  if (ob)
    x = -x;
}

const int N = 201000;
typedef pair<int, int> pii;
pii b[N];
int st[N];
int n, R, C, tp, tot;

inline int get(int x, int y) {
  if (x and x != R and y and y != C)
    return -1;
  if (y == 0)
    return x;
  if (x == R)
    return R + y;
  if (y == C)
    return R + C + (R - x);
  return R + C + R + (C - y);
}

int main() {
  //	freopen("in","r",stdin);
  read(R), read(C), read(n);
  for (int i = 1, x, y, t1, t2; i <= n; ++i) {
    read(x), read(y), t1 = get(x, y);
    read(x), read(y), t2 = get(x, y);
    if (t1 == -1 or t2 == -1)
      continue;
    b[++tot] = pii(t1, i);
    b[++tot] = pii(t2, i);
  }
  sort(b + 1, b + tot + 1);
  for (int i = 1; i <= tot; ++i)
    if (tp and st[tp] == b[i].second)
      --tp;
    else
      st[++tp] = b[i].second;
  puts(tp ? "NO" : "YES");
  return 0;
}
Problem p03685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
int r, c, n, sx, sy, tx, ty, cnt, x, y, tp, st[100005];
struct note {
  int x, id, type;
} a[100005];
int cal(int x, int y) {
  if (x == 0)
    return y;
  if (x == c)
    return r + c + r - y;
  if (y == 0)
    return r + c + r + c - x;
  if (y == r)
    return r + x;
  return -1;
}
bool cmp(note x, note y) { return x.x < y.x; }
int main() {
  scanf("%d%d%d", &c, &r, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    x = cal(sx, sy);
    y = cal(tx, ty);
    if (x == -1 || y == -1)
      continue;
    if (x > y)
      std::swap(x, y);
    a[++cnt] = {x, i, 1};
    a[++cnt] = {y, i, 2};
  }
  std::sort(a + 1, a + cnt + 1, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (a[i].type == 1)
      st[++tp] = a[i].id;
    else {
      if (st[tp] != a[i].id) {
        puts("NO");
        return 0;
      }
      tp--;
    }
  }
  puts("YES");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
int r, c, n, sx, sy, tx, ty, cnt, x, y, tp, st[100005];
struct note {
  int x, id, type;
} a[200005];
int cal(int x, int y) {
  if (x == 0)
    return y;
  if (x == c)
    return r + c + r - y;
  if (y == 0)
    return r + c + r + c - x;
  if (y == r)
    return r + x;
  return -1;
}
bool cmp(note x, note y) { return x.x < y.x; }
int main() {
  scanf("%d%d%d", &c, &r, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    x = cal(sx, sy);
    y = cal(tx, ty);
    if (x == -1 || y == -1)
      continue;
    if (x > y)
      std::swap(x, y);
    a[++cnt] = {x, i, 1};
    a[++cnt] = {y, i, 2};
  }
  std::sort(a + 1, a + cnt + 1, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (a[i].type == 1)
      st[++tp] = a[i].id;
    else {
      if (st[tp] != a[i].id) {
        puts("NO");
        return 0;
      }
      tp--;
    }
  }
  puts("YES");
  return 0;
}
Problem p03685 (C++)

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

int R, C, N, x1, x2, y1, y2;

int online(int x, int y) {
  if (x == 0)
    return 1;
  if (y == C)
    return 2;
  if (x == R)
    return 3;
  if (y == 0)
    return 4;
  return -1;
}

int main() {
  cin >> R >> C >> N;
  vector<pair<pair<int, int>, int>> v;
  for (int i = 1; i <= N; i++) {
    cin >> x1 >> y1 >> x2 >> y2;
    int a = online(x1, y1), b = online(x2, y2);
    if (a != -1 && b != -1) {
      if (a == 1)
        v.push_back({{1, y1}, i});
      if (a == 2)
        v.push_back({{2, x1}, i});
      if (a == 3)
        v.push_back({{3, -y1}, i});
      if (a == 4)
        v.push_back({{4, -x1}, i});
      if (b == 1)
        v.push_back({{1, y2}, i});
      if (b == 2)
        v.push_back({{2, x2}, i});
      if (b == 3)
        v.push_back({{3, -y2}, i});
      if (b == 4)
        v.push_back({{4, -x2}, i});
    }
  }
  sort(v.begin(), v.end());
  stack<int> s;
  for (int i = 0; i < v.size(); i++) {
    if (i == 0) {
      s.push(v[i].second);
      continue;
    }
    if (v[i].second != s.top())
      s.push(v[i].second);
    else
      s.pop();
  }
  cout << (s.empty() ? "YES" : "NO") << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int R, C, N, x1, x2, y1, y2;

int online(int x, int y) {
  if (x == 0)
    return 1;
  if (y == C)
    return 2;
  if (x == R)
    return 3;
  if (y == 0)
    return 4;
  return -1;
}

int main() {
  cin >> R >> C >> N;
  vector<pair<pair<int, int>, int>> v;
  for (int i = 1; i <= N; i++) {
    cin >> x1 >> y1 >> x2 >> y2;
    int a = online(x1, y1), b = online(x2, y2);
    if (a != -1 && b != -1) {
      if (a == 1)
        v.push_back({{1, y1}, i});
      if (a == 2)
        v.push_back({{2, x1}, i});
      if (a == 3)
        v.push_back({{3, -y1}, i});
      if (a == 4)
        v.push_back({{4, -x1}, i});
      if (b == 1)
        v.push_back({{1, y2}, i});
      if (b == 2)
        v.push_back({{2, x2}, i});
      if (b == 3)
        v.push_back({{3, -y2}, i});
      if (b == 4)
        v.push_back({{4, -x2}, i});
    }
  }
  sort(v.begin(), v.end());
  stack<int> s;
  for (int i = 0; i < v.size(); i++) {
    if (s.empty()) {
      s.push(v[i].second);
      continue;
    }
    if (v[i].second != s.top())
      s.push(v[i].second);
    else
      s.pop();
  }
  cout << (s.empty() ? "YES" : "NO") << endl;
}
Problem p03685 (C++)

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

int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
  return x;
}

/*============ Header Template ============*/

int R, C, n;
struct point {
  int x, num;
} p[200005];
int cnt;
bool isedge(int x, int y) {
  if (x == 0 || x == R || y == 0 || y == C)
    return 1;
  return 0;
}
bool isimport(int x, int y, int X, int Y) {
  return (isedge(x, y) & isedge(X, Y));
}
int get(int x, int y) {
  if (y == 0)
    return x;
  if (x == R)
    return R + y;
  if (y == C)
    return R + C + R - x;
  if (x == 0)
    return R + C + R + C - y;
  return -1;
}
bool cmp(point a, point b) { return a.x < b.x; }
void judge() {
  stack<int> sta;
  sort(p + 1, p + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (sta.empty())
      sta.push(p[i].num);
    else if (sta.top() == p[i].num)
      sta.pop();
    else
      sta.push(p[i].num);
  }
  if (!sta.empty())
    puts("NO");
  else
    puts("YES");
}
int main() {
  freopen("A.in", "r", stdin);
  R = read();
  C = read();
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read(), X = read(), Y = read();
    if (isimport(x, y, X, Y)) {
      p[++cnt].x = get(x, y);
      p[cnt].num = i;
      p[++cnt].x = get(X, Y);
      p[cnt].num = i;
    }
  }
  judge();
  return 0;
}
/*
2 2 4
0 0 2 2
2 0 0 1
0 2 1 2
1 1 2 1
*/

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

int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
  return x;
}

/*============ Header Template ============*/

int R, C, n;
struct point {
  int x, num;
} p[200005];
int cnt;
bool isedge(int x, int y) {
  if (x == 0 || x == R || y == 0 || y == C)
    return 1;
  return 0;
}
bool isimport(int x, int y, int X, int Y) {
  return (isedge(x, y) & isedge(X, Y));
}
int get(int x, int y) {
  if (y == 0)
    return x;
  if (x == R)
    return R + y;
  if (y == C)
    return R + C + R - x;
  if (x == 0)
    return R + C + R + C - y;
  return -1;
}
bool cmp(point a, point b) { return a.x < b.x; }
void judge() {
  stack<int> sta;
  sort(p + 1, p + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) {
    if (sta.empty())
      sta.push(p[i].num);
    else if (sta.top() == p[i].num)
      sta.pop();
    else
      sta.push(p[i].num);
  }
  if (!sta.empty())
    puts("NO");
  else
    puts("YES");
}
int main() {
  R = read();
  C = read();
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read(), X = read(), Y = read();
    if (isimport(x, y, X, Y)) {
      p[++cnt].x = get(x, y);
      p[cnt].num = i;
      p[++cnt].x = get(X, Y);
      p[cnt].num = i;
    }
  }
  judge();
  return 0;
}
/*
2 2 4
0 0 2 2
2 0 0 1
0 2 1 2
1 1 2 1
*/
Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define MN 110000
using namespace std;

int read_p, read_ca;
inline int read() {
  read_p = 0;
  read_ca = getchar();
  while (read_ca < '0' || read_ca > '9')
    read_ca = getchar();
  while (read_ca >= '0' && read_ca <= '9')
    read_p = read_p * 10 + read_ca - 48, read_ca = getchar();
  return read_p;
}
struct na {
  int l, r;
} p[MN];
bool cmpl(na a, na b) { return a.l == b.l ? a.r > b.r : a.l < b.l; }
priority_queue<int, vector<int>, greater<int>> q, _q;
int n, m, mmh;
int main() {
  mmh = n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    p[i].l = read(), p[i].r = read();
  sort(p + 1, p + 1 + n, cmpl);
  for (int i = 1; i <= n; i++)
    if (p[i].l == q.size())
      q.push(p[i].r), _q.push(q.top()), q.pop();
    else
      q.push(p[i].r), mmh--;
  for (int i = q.size() + 1; i <= m && !_q.empty(); i++)
    if (_q.top() <= i)
      _q.pop(), mmh--;
  printf("%d\n", mmh);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define MN 210000
using namespace std;

int read_p, read_ca;
inline int read() {
  read_p = 0;
  read_ca = getchar();
  while (read_ca < '0' || read_ca > '9')
    read_ca = getchar();
  while (read_ca >= '0' && read_ca <= '9')
    read_p = read_p * 10 + read_ca - 48, read_ca = getchar();
  return read_p;
}
struct na {
  int l, r;
} p[MN];
bool cmpl(na a, na b) { return a.l == b.l ? a.r > b.r : a.l < b.l; }
priority_queue<int, vector<int>, greater<int>> q, _q;
int n, m, mmh;
int main() {
  mmh = n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    p[i].l = read(), p[i].r = read();
  sort(p + 1, p + 1 + n, cmpl);
  for (int i = 1; i <= n; i++)
    if (p[i].l == q.size())
      q.push(p[i].r), _q.push(q.top()), q.pop();
    else
      q.push(p[i].r), mmh--;
  for (int i = q.size() + 1; i <= m && !_q.empty(); i++)
    if (_q.top() <= i)
      _q.pop(), mmh--;
  printf("%d\n", mmh);
}
Problem p03686 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int L, R, n, m;
priority_queue<int> q;
struct nod {
  int l, r;
  friend bool operator<(nod a, nod b) {
    if (a.l != b.l)
      return a.l < b.l;
    return a.r > b.r;
  }
} a[100005];
int main() {
  scanf("%d%d", &n, &m);
  R = m + 1;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (a[i].l > L && L < m)
      L++;
    else
      q.push(a[i].r);
  while (!q.empty()) {
    if (R - L == 1)
      break;
    int x = q.top();
    q.pop();
    if (R > x)
      R--;
  }
  printf("%d\n", n - (L + (m - R + 1)));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int L, R, n, m;
priority_queue<int> q;
struct nod {
  int l, r;
  friend bool operator<(nod a, nod b) {
    if (a.l != b.l)
      return a.l < b.l;
    return a.r > b.r;
  }
} a[200005];
int main() {
  scanf("%d%d", &n, &m);
  R = m + 1;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (a[i].l > L && L < m)
      L++;
    else
      q.push(a[i].r);
  while (!q.empty()) {
    if (R - L == 1)
      break;
    int x = q.top();
    q.pop();
    if (R > x)
      R--;
  }
  printf("%d\n", n - (L + (m - R + 1)));
}
Problem p03686 (C++)

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

vector<int> vec[101000];
int n, m, x, y, ans, t;
priority_queue<int> q;

int main() {
  cin.tie(0);
  cin >> n >> m;
  for (int i = n; i--;)
    cin >> x >> y, vec[x].push_back(y);
  for (int i = m; ~i; --i) {
    for (auto o : vec[i])
      q.push(o);
    if (i && !q.empty())
      q.pop(), ++t;
  }
  ans = t;
  y = m;
  for (; !q.empty();) {
    x = q.top();
    q.pop();
    if (x <= y && y > t)
      ++ans, --y;
  }
  cout << n - ans << endl;
}


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

vector<int> vec[201000];
int n, m, x, y, ans, t;
priority_queue<int> q;

int main() {
  cin.tie(0);
  cin >> n >> m;
  for (int i = n; i--;)
    cin >> x >> y, vec[x].push_back(y);
  for (int i = m; ~i; --i) {
    for (auto o : vec[i])
      q.push(o);
    if (i && !q.empty())
      q.pop(), ++t;
  }
  ans = t;
  y = m;
  for (; !q.empty();) {
    x = q.top();
    q.pop();
    if (x <= y && y > t)
      ++ans, --y;
  }
  cout << n - ans << endl;
}
Problem p03686 (C++)

Buggy submission (status: Runtime Error):
// Exhausted?
// * frank_c1
// * 2017 / 11 / 16

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

#define lowbit(x) ((x) & (-x))

const int maxn = (int)(1e5) + 5;
int mx[maxn], su[maxn];
vector<int> nxt[maxn];
int n, m;

inline void addX(int x, int v) {
  mx[x] += v;
  su[x] += v;
}

inline void pd(int x) {
  if (su[x]) {
    addX(x << 1, su[x]);
    addX(x << 1 | 1, su[x]);
    su[x] = 0;
  }
}

inline void pu(int x) { mx[x] = max(mx[x << 1], mx[x << 1 | 1]); }

int Q(int x, int l, int r, int p) {
  if (r <= p)
    return mx[x];
  pd(x);
  int mid = (l + r) >> 1, ret = Q(x << 1, l, mid, p);
  if (mid < p)
    ret = max(ret, Q(x << 1 | 1, mid + 1, r, p));
  return ret;
}

void M(int x, int l, int r, int p, int v) {
  if (p <= l) {
    addX(x, v);
    return;
  }
  pd(x);
  int mid = (l + r) >> 1;
  if (p <= mid)
    M(x << 1, l, mid, p, v);
  M(x << 1 | 1, mid + 1, r, p, v);
  pu(x);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    nxt[l].push_back(m - r + 1);
  }
  int ret = max(0, n - m);
  for (int i = 1; i <= m; ++i)
    M(1, 0, m, i, -1);
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j < (int)nxt[i].size(); ++j)
      M(1, 0, m, nxt[i][j], 1);
    ret = max(ret, Q(1, 0, m, m - i) - i);
  }
  return printf("%d\n", ret), 0;
}

Fixed submission:
// Exhausted?
// * frank_c1
// * 2017 / 11 / 16

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

#define lowbit(x) ((x) & (-x))

const int maxn = (int)(2e5) + 5;
int mx[maxn << 2], su[maxn << 2];
vector<int> nxt[maxn];
int n, m;

inline void addX(int x, int v) {
  mx[x] += v;
  su[x] += v;
}

inline void pd(int x) {
  if (su[x]) {
    addX(x << 1, su[x]);
    addX(x << 1 | 1, su[x]);
    su[x] = 0;
  }
}

inline void pu(int x) { mx[x] = max(mx[x << 1], mx[x << 1 | 1]); }

int Q(int x, int l, int r, int p) {
  if (r <= p)
    return mx[x];
  pd(x);
  int mid = (l + r) >> 1, ret = Q(x << 1, l, mid, p);
  if (mid < p)
    ret = max(ret, Q(x << 1 | 1, mid + 1, r, p));
  return ret;
}

void M(int x, int l, int r, int p, int v) {
  if (p <= l) {
    addX(x, v);
    return;
  }
  pd(x);
  int mid = (l + r) >> 1;
  if (p <= mid)
    M(x << 1, l, mid, p, v);
  M(x << 1 | 1, mid + 1, r, p, v);
  pu(x);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    nxt[l].push_back(m - r + 1);
  }
  int ret = max(0, n - m);
  for (int i = 1; i <= m; ++i)
    M(1, 0, m, i, -1);
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j < (int)nxt[i].size(); ++j)
      M(1, 0, m, nxt[i][j], 1);
    ret = max(ret, Q(1, 0, m, m - i) - i);
  }
  return printf("%d\n", ret), 0;
}
Problem p03687 (C++)

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

#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;

string s;
vector<int> a[30];

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);

#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  cin >> s;

  for (int i = 0; i < 26; i++)
    a[i].pb(-1);

  for (int i = 0; i < (int)s.size(); i++)
    a[s[i] - 'a'].pb(i);

  for (int i = 0; i < 26; i++)
    a[i].pb(s.size());

  int ans = s.size();

  for (int i = 0; i < 26; i++) {
    int lans = 0;

    for (int j = 1; j < a[i].size(); j++)
      lans = max(lans, a[i][j] - a[i][j - 1] - 1);

    ans = min(ans, lans);
  }

  cout << ans;
  return 0;
}

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

#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;

string s;
vector<int> a[30];

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> s;

  for (int i = 0; i < 26; i++)
    a[i].pb(-1);

  for (int i = 0; i < (int)s.size(); i++)
    a[s[i] - 'a'].pb(i);

  for (int i = 0; i < 26; i++)
    a[i].pb(s.size());

  int ans = s.size();

  for (int i = 0; i < 26; i++) {
    int lans = 0;

    for (int j = 1; j < a[i].size(); j++)
      lans = max(lans, a[i][j] - a[i][j - 1] - 1);

    ans = min(ans, lans);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string t;
  cin >> t;

  if (t.size() == 1) {
    cout << 0 << endl;
  }

  int ans = 114514;
  for (int i = 'a'; i <= 'z'; ++i) {
    string t1, t2 = t;
    int cnt = 0;
    while (1) {
      t1 = "";
      for (int j = 0; j < t2.size() - 1; ++j) {
        if (t2[j] == i || t2[j + 1] == i) {
          t1 += i;
        } else {
          t1 += t2[j];
        }
      }

      bool check = true;
      for (int j = 0; j < t1.size() - 1; ++j) {
        if (t1[j] != t1[j + 1]) {
          check = false;
        }
      }
      if (check) {
        break;
      }

      cnt++;
      t2 = t1;
    }
    ans = min(ans, cnt);
  }

  cout << (ans == 0 ? 0 : ans + 1) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string t;
  cin >> t;

  if (t.size() == 1) {
    cout << 0 << endl;
    return 0;
  }

  int ans = 114514;
  for (int i = 'a'; i <= 'z'; ++i) {
    string t1, t2 = t;
    int cnt = 0;
    while (1) {
      t1 = "";
      for (int j = 0; j < t2.size() - 1; ++j) {
        if (t2[j] == i || t2[j + 1] == i) {
          t1 += i;
        } else {
          t1 += t2[j];
        }
      }

      bool check = true;
      for (int j = 0; j < t1.size() - 1; ++j) {
        if (t1[j] != t1[j + 1]) {
          check = false;
        }
      }
      if (check) {
        break;
      }

      cnt++;
      t2 = t1;
    }
    ans = min(ans, cnt);
  }

  cout << (ans == 0 ? 0 : ans + 1) << endl;

  return 0;
}
Problem p03687 (C++)

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

using namespace std;

bool check(string s) {
  bool all = true;
  for (int j = 1; j < s.size(); j++) {
    if (s[j] != s[0])
      all = false;
  }
  return all;
}

int main() {
  string s;
  cin >> s;
  string tmp = s;
  int ans = 11111111;
  int num = 0;
  for (int i = 0; i < 26; i++) {
    tmp = s;
    num = 0;
    bool update = true;
    while (update) {
      update = false;
      if (tmp.size() == 1)
        break;
      for (int j = 0; j < s.size() - 1; j++) {
        // 最終文字まで置換
        if (tmp[j] == 'a' + i) {
          // そのまま
          update = true;
        } else if (tmp[j + 1] == 'a' + i) {
          tmp[j] = tmp[j + 1];
          update = true;
        }
      }
      tmp = tmp.substr(0, tmp.size() - 1);
      if (update)
        num++;

      if (check(tmp))
        break;

      // cout<<"["<<num<<"]:"<<tmp<<endl;
    }

    if (check(tmp)) {
      ans = min(ans, num);
    }
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

bool check(string s) {
  bool all = true;
  for (int j = 1; j < s.size(); j++) {
    if (s[j] != s[0])
      all = false;
  }
  return all;
}

int main() {
  string s;
  cin >> s;
  string tmp = s;
  int ans = 11111111;
  int num = 0;
  for (int i = 0; i < 26; i++) {
    tmp = s;
    num = 0;
    bool update = true;
    while (update) {
      update = false;
      if (tmp.size() == 1)
        break;
      for (int j = 0; j < tmp.size() - 1; j++) {
        // 最終文字まで置換
        if (tmp[j] == 'a' + i) {
          // そのまま
          update = true;
        } else if (tmp[j + 1] == 'a' + i) {
          tmp[j] = tmp[j + 1];
          update = true;
        }
      }
      tmp = tmp.substr(0, tmp.size() - 1);
      if (update)
        num++;

      if (check(tmp))
        break;

      // cout<<"["<<num<<"]:"<<tmp<<endl;
    }

    if (check(tmp)) {
      ans = min(ans, num);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03687 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000

using namespace std;

int main(void) {
  bool flag = false;
  int check = 0;
  int count = 0;
  int total = INF;
  string s;
  string p;
  string t;

  cin >> s;
  p = s;
  t = s;

  FOR(i, 0, 25) {
    count = 0;
    check = 0;
    flag = false;
    s = p;
    t = s;
    FOR(u, 0, s.size() - 2) {
      if (t[u] != 'a' + i) {
        break;
      }
      if (u == s.size() - 2) {
        total = 0;
        break;
      }
    }
    while (1) {
      FOR(u, 0, s.size() - 2) {
        if (s[u] == 'a' + i || s[u + 1] == 'a' + i) {
          check++;
          t[u] = 'a' + i;
        }
      }
      count++;
      if (check == 0) {
        break;
      }
      t.pop_back();
      FOR(u, 0, s.size() - 2) {
        if (t[u] != 'a' + i) {
          break;
        }
        if (u == s.size() - 2) {
          flag = true;
        }
      }
      s = t;

      if (flag == true) {
        if (count < total) {
          total = count;
        }
        break;
        s = p;
        t = s;
      }
    }
  }
  cout << total << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000

using namespace std;

int main(void) {
  bool flag = false;
  int check = 0;
  int count = 0;
  int total = INF;
  string s;
  string p;
  string t;

  cin >> s;
  p = s;
  t = s;

  FOR(i, 0, 25) {
    count = 0;
    check = 0;
    flag = false;
    s = p;
    t = s;
    if (s.size() == 1) {
      total = 0;
      break;
    }
    FOR(u, 0, s.size() - 2) {
      if (t[u] != 'a' + i) {
        break;
      }
      if (u == s.size() - 2) {
        total = 0;
        break;
      }
    }
    while (1) {
      FOR(u, 0, s.size() - 2) {
        if (s[u] == 'a' + i || s[u + 1] == 'a' + i) {
          check++;
          t[u] = 'a' + i;
        }
      }
      count++;
      if (check == 0) {
        break;
      }
      t.pop_back();
      FOR(u, 0, s.size() - 2) {
        if (t[u] != 'a' + i) {
          break;
        }
        if (u == s.size() - 2) {
          flag = true;
        }
      }
      s = t;

      if (flag == true) {
        if (count < total) {
          total = count;
        }
        break;
        s = p;
        t = s;
      }
    }
  }
  cout << total << endl;
  return 0;
}
Problem p03688 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int a[100000 + 10], n;
int cnt;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  if (a[n] - a[1] > 1)
    return printf("No"), 0;
  if (a[1] == a[n])
    return printf("%s", n == a[1] + 1 || n >= a[1] * 2 ? "Yes" : "No"), 0;
  // if (a[1]    )
  for (int i = 1; i <= n; ++i)
    if (a[i] == a[1])
      ++cnt;
  if (cnt > a[1])
    return printf("No"), 0;
  return printf("%s", (n - cnt) >= (a[n] - cnt) * 2 ? "Yes" : "No");
  // return printf("%s",a[1]==a[2]?:)
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int a[100000 + 10], n;
int cnt;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  if (a[n] - a[1] > 1)
    return printf("No"), 0;
  if (a[1] == a[n])
    return printf("%s", n == a[1] + 1 || n >= a[1] * 2 ? "Yes" : "No"), 0;
  // if (a[1]    )
  for (int i = 1; i <= n; ++i)
    if (a[i] == a[1])
      ++cnt;
  if (cnt > a[1])
    return printf("No"), 0;
  return printf("%s", (n - cnt) >= (a[n] - cnt) * 2 ? "Yes" : "No"), 0;
  // return printf("%s",a[1]==a[2]?:)
}
Problem p03688 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print("No")
    exit()

if len(A.keys()) == 1:
    p = list(A.keys())[0]
    print("Yes" if 2 * p <= N or p == N - 1 else "No")
    exit()

p1, p2 = sorted(A.keys())
1 / 0
print("Yes" if [p1, p2] == [1, 2] and A[1] == 1 else "No")


Fixed submission:
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print("No")
    exit()

if len(A.keys()) == 1:
    p = list(A.keys())[0]
    print("Yes" if 2 * p <= N or p == N - 1 else "No")
    exit()

p1, p2 = sorted(A.keys())
print("Yes" if p2 - p1 == 1 and (A[p1] + 1) <= p2 <= (A[p1] + A[p2] // 2) else "No")
Problem p03688 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 10010
#define No                                                                     \
  printf("No\n");                                                              \
  return 0;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int T, n, a[N], vis[N], p[N], tot;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  bool ok = true;
  tot = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= n || a[i] < 1) {
      ok = false;
      break;
    }
    ++vis[a[i]];
    if (vis[a[i]] == 1) {
      p[++tot] = a[i];
      if (tot > 2) {
        ok = false;
        break;
      }
    }
  }
  if (!ok) {
    No;
  }
  bool tmp = false;
  if (tot == 1) {
    if (p[1] == n - 1 || p[1] <= n / 2) {
      tmp = true;
    }
  } else {
    if (p[1] > p[2])
      swap(p[1], p[2]);
    if (p[1] + 1 == p[2]) {
      int res = p[2] - vis[p[1]];
      if (res >= 1 && res <= vis[p[2]] / 2) {
        tmp = true;
      }
    }
  }
  if (tmp) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 100100
#define No                                                                     \
  printf("No\n");                                                              \
  return 0;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int T, n, a[N], vis[N], p[N], tot;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  bool ok = true;
  tot = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= n || a[i] < 1) {
      ok = false;
      break;
    }
    ++vis[a[i]];
    if (vis[a[i]] == 1) {
      p[++tot] = a[i];
      if (tot > 2) {
        ok = false;
        break;
      }
    }
  }
  if (!ok) {
    No;
  }
  bool tmp = false;
  if (tot == 1) {
    if (p[1] == n - 1 || p[1] <= n / 2) {
      tmp = true;
    }
  } else {
    if (p[1] > p[2])
      swap(p[1], p[2]);
    if (p[1] + 1 == p[2]) {
      int res = p[2] - vis[p[1]];
      if (res >= 1 && res <= vis[p[2]] / 2) {
        tmp = true;
      }
    }
  }
  if (tmp) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}
Problem p03688 (C++)

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

long long N;
vector<long long> a;
set<long long> memo;

bool check(long long N) {
  // cout << "N = " << N << endl;

  long long nnum = 0;
  long long nonenum = 0;
  for (long long i = 0; i < a.size(); i++) {
    if (a[i] == N - 1) {
      nonenum++;
    } else if (a[i] == N) {
      nnum++;
    } else {
      exit(1);
    }
  }
  // cout << "n - 1 = " << nonenum << " nnum = " << nnum << endl;
  if (nnum == 1) {
    return false;
  }

  long long mini;
  long long maxi;
  if (nnum > 1) {
    mini = nonenum + 1;
    maxi = nonenum + nnum / 2;
  } else {
    mini = nonenum;
    maxi = nonenum + nnum / 2;
  }
  return mini <= N && N <= maxi;
}

int main() {
  cin >> N;
  for (long long i = 0; i < N; i++) {
    long long tmp;
    cin >> tmp;
    a.push_back(tmp);
    memo.insert(tmp);
  }
  sort(a.begin(), a.end());

  if (memo.size() > 2) {
    cout << "No" << endl;
    return 0;
  }

  if (memo.size() == 1) {
    if (check(a[0]) || check(a[0] + 1)) {
      cout << "Yes" << endl;
      return 0;
    } else {
      cout << "No" << endl;
      return 0;
    }
  } else {
    if (check(a[a.size() - 1])) {
      cout << "Yes" << endl;
      return 0;
    } else {
      cout << "No" << endl;
      return 0;
    }
  }
}


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

long long N;
vector<long long> a;
set<long long> memo;

bool check(long long N) {
  // cout << "N = " << N << endl;

  long long nnum = 0;
  long long nonenum = 0;
  for (long long i = 0; i < a.size(); i++) {
    if (a[i] == N - 1) {
      nonenum++;
    } else if (a[i] == N) {
      nnum++;
    } else {
      exit(1);
    }
  }
  // cout << "n - 1 = " << nonenum << " nnum = " << nnum << endl;
  if (nnum == 1) {
    return false;
  }

  long long mini;
  long long maxi;
  if (nnum > 1) {
    mini = nonenum + 1;
    maxi = nonenum + nnum / 2;
  } else {
    mini = nonenum;
    maxi = nonenum + nnum / 2;
  }
  return mini <= N && N <= maxi;
}

int main() {
  cin >> N;
  for (long long i = 0; i < N; i++) {
    long long tmp;
    cin >> tmp;
    a.push_back(tmp);
    memo.insert(tmp);
  }
  sort(a.begin(), a.end());

  if (memo.size() != 1 && memo.size() != 2) {
    cout << "No" << endl;
    return 0;
  }

  if (a[a.size() - 1] - a[0] >= 2) {
    cout << "No" << endl;
    return 0;
  }

  if (memo.size() == 1) {
    if (check(a[0]) || check(a[0] + 1)) {
      cout << "Yes" << endl;
      return 0;
    } else {
      cout << "No" << endl;
      return 0;
    }
  } else {
    if (check(a[a.size() - 1])) {
      cout << "Yes" << endl;
      return 0;
    } else {
      cout << "No" << endl;
      return 0;
    }
  }
}
Problem p03688 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

typedef long long ll;

int n;
int vs[100000 + 10];

bool go() {
  sort(vs, vs + n);
  if ((vs[0] == vs[n - 1] || vs[0] + 1 == vs[n - 1]) ==
      false) { // 3種類以上の証言
    return false;
  }
  int c = vs[0];
  int k = 0;
  REP(i, n) {
    if (vs[i] == c) {
      ++k;
    }
  }
  if (n == 2) {
    assert(false);
    return c == 1 && k == 2;
  } else if (n == 3) {
    return !(c == 1 && k == 2);
  } else if (k == n) {
    return 2 * c <= n;
  } else {
    return c >= k && k >= max(2 * c - n + 2, 0);
  }
}

int main(void) {
  scanf("%d", &n);
  REP(i, n) { scanf("%d", &vs[i]); }
  puts(go() ? "Yes" : "No");

  return 0;
}


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

typedef long long ll;

int n;
int vs[100000 + 10];

bool go() {
  sort(vs, vs + n);
  if ((vs[0] == vs[n - 1] || vs[0] + 1 == vs[n - 1]) ==
      false) { // 3種類以上の証言
    return false;
  }
  int c = vs[0];
  int k = 0;
  REP(i, n) {
    if (vs[i] == c) {
      ++k;
    }
  }
  if (k == n) {
    return 2 * c <= n || c == n - 1;
  } else {
    return c >= k && k >= max(2 * c - n + 2, 0);
  }
}

int main(void) {
  scanf("%d", &n);
  REP(i, n) { scanf("%d", &vs[i]); }
  puts(go() ? "Yes" : "No");

  return 0;
}
Problem p03688 (C++)

Buggy submission (status: Runtime Error):
// This code wrote by chtholly_micromaker(MicroMaker)
#include <bits/stdc++.h>
#define reg register
using namespace std;
template <class t> inline void read(t &s) {
  s = 0;
  reg int f = 1;
  reg char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  s *= f;
  return;
}
int a[10050];
int n;
/*
inline bool all_same()
{
        for(int i=1;i<n;++i)
                if(a[i]!=a[i+1])
                        return false;
        return true;
}
*/
inline void work() {
  cin >> n;
  reg int mini = 0x3f3f3f3f, maxi = 0;
  for (int i = 1; i <= n; ++i)
    read(a[i]), mini = min(mini, a[i]), maxi = max(maxi, a[i]);
  if (mini > n - 1) {
    puts("Yes");
    return;
  }
  // if(all_same())
  if (maxi == mini) {
    if (a[1] == 1 || a[1] == n - 1) {
      puts("Yes");
      return;
    }
    if (a[1] <= n / 2)
      puts("Yes");
    else
      puts("No");
    return;
  }
  if (maxi - mini >= 2) {
    puts("No");
    return;
  }
  reg int cntmax = 0, cntmin = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == mini)
      ++cntmin;
    if (a[i] == maxi)
      ++cntmax;
  }
  if (mini < cntmin) {
    puts("No");
    return;
  }
  if (2 * mini < n + cntmin - 1) {
    puts("Yes");
    return;
  }
  puts("No");
  return;
}
signed main(void) {
  // int t;cin>>t;
  // while(t--)
  work();
  return 0;
}


Fixed submission:
// This code wrote by chtholly_micromaker(MicroMaker)
#include <bits/stdc++.h>
#define reg register
using namespace std;
template <class t> inline void read(t &s) {
  s = 0;
  reg int f = 1;
  reg char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
  s *= f;
  return;
}
int a[100050];
int n;
/*
inline bool all_same()
{
        for(int i=1;i<n;++i)
                if(a[i]!=a[i+1])
                        return false;
        return true;
}
*/
inline void work() {
  cin >> n;
  reg int mini = 0x3f3f3f3f, maxi = 0;
  for (int i = 1; i <= n; ++i)
    read(a[i]), mini = min(mini, a[i]), maxi = max(maxi, a[i]);
  if (mini > n - 1) {
    puts("Yes");
    return;
  }
  // if(all_same())
  if (maxi == mini) {
    if (a[1] == 1 || a[1] == n - 1) {
      puts("Yes");
      return;
    }
    if (a[1] <= n / 2)
      puts("Yes");
    else
      puts("No");
    return;
  }
  if (maxi - mini >= 2) {
    puts("No");
    return;
  }
  reg int cntmax = 0, cntmin = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == mini)
      ++cntmin;
    if (a[i] == maxi)
      ++cntmax;
  }
  if (mini < cntmin) {
    puts("No");
    return;
  }
  if (2 * mini < n + cntmin - 1) {
    puts("Yes");
    return;
  }
  puts("No");
  return;
}
signed main(void) {
  // int t;cin>>t;
  // while(t--)
  work();
  return 0;
}
Problem p03688 (C++)

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

int main() {
  int n, cont3 = 0;
  cin >> n;
  int v[n], val[2];
  val[0] = val[1] = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (ok[v[i]] == false) {
      ok[v[i]] = true;
      val[cont3] = v[i];
      cont3++;
      if (cont3 == 3) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  if (cont3 == 1) {
    if (n == val[0] + 1 || n >= val[0] * 2)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  } else {

    int cont = 0, cont1 = 0;
    if (val[0] > val[1])
      swap(val[0], val[1]);

    for (int i = 0; i < n; i++) {
      if (v[i] == val[0])
        cont++;
      else
        cont1++;
    }
    if (val[0] == val[1] - 1 && cont1 != 1 && val[1] > cont &&
        val[1] <= cont + (cont1 / 2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
map<int, bool> ok;

int main() {
  int n, cont3 = 0;
  cin >> n;
  int v[n], val[2];
  val[0] = val[1] = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (ok[v[i]] == false) {
      ok[v[i]] = true;
      val[cont3] = v[i];
      cont3++;
      if (cont3 == 3) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  if (cont3 == 1) {
    if (n == val[0] + 1 || n >= val[0] * 2)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  } else {

    int cont = 0, cont1 = 0;
    if (val[0] > val[1])
      swap(val[0], val[1]);

    for (int i = 0; i < n; i++) {
      if (v[i] == val[0])
        cont++;
      else
        cont1++;
    }
    if (val[0] == val[1] - 1 && cont1 != 1 && val[1] > cont &&
        val[1] <= cont + (cont1 / 2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}
Problem p03688 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ts cout << "ok" << endl
#define int long long
#define hh puts("")
#define pc putchar
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) char
// buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N = 10005;
int n, a[N];
inline int read() {
  int ret = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + (ch ^ 48);
    ch = getchar();
  }
  return ret * ff;
}
void write(int x) {
  if (x < 0) {
    x = -x, pc('-');
  }
  if (x > 9)
    write(x / 10);
  pc(x % 10 + 48);
}
void writeln(int x) { write(x), hh; }
void writesp(int x) { write(x), pc(' '); }
signed main() {
  n = read();
  int mx = 0, mn = 1e9;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
  }
  if (mx - mn > 1)
    puts("No");
  else if (mx == mn) {
    if (mx <= n / 2 || mx == n - 1)
      puts("Yes");
    else
      puts("No");
  } else {
    int c1 = 0, c2 = 0;
    for (int i = 1; i <= n; i++) {
      c1 += (a[i] == mn);
      c2 += (a[i] == mx);
    }
    if (mx - c1 >= 1 && mx - c1 <= c2 / 2)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ts cout << "ok" << endl
#define int long long
#define hh puts("")
#define pc putchar
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) char
// buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N = 100005;
int n, a[N];
inline int read() {
  int ret = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + (ch ^ 48);
    ch = getchar();
  }
  return ret * ff;
}
void write(int x) {
  if (x < 0) {
    x = -x, pc('-');
  }
  if (x > 9)
    write(x / 10);
  pc(x % 10 + 48);
}
void writeln(int x) { write(x), hh; }
void writesp(int x) { write(x), pc(' '); }
signed main() {
  n = read();
  int mx = 0, mn = 1e9;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
  }
  if (mx - mn > 1)
    puts("No");
  else if (mx == mn) {
    if (mx <= n / 2 || mx == n - 1)
      puts("Yes");
    else
      puts("No");
  } else {
    int c1 = 0, c2 = 0;
    for (int i = 1; i <= n; i++) {
      c1 += (a[i] == mn);
      c2 += (a[i] == mx);
    }
    if (mx - c1 >= 1 && mx - c1 <= c2 / 2)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}
Problem p03688 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100001
using namespace std;
typedef pair<int, int> P;

int A[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  A[n] = 1e9;
  sort(A, A + n);

  if (A[0] == n - 1)
    cout << "Yes" << endl, exit(0);
  if (A[n - 1] - A[0] >= 2)
    cout << "No" << endl, exit(0);
  if (A[0] == A[n - 1]) {
    if (n / A[0] < 2)
      cout << "No" << endl, exit(0);
    cout << "Yes" << endl, exit(0);
  }

  int idx = 0;
  while (idx < n && A[idx] == A[idx + 1])
    idx++;
  if (idx > 0 && A[0] == idx + 1)
    cout << "Yes" << endl, exit(0);
  if (idx > 0 && A[0] < idx + 1)
    cout << "No" << endl, exit(1);
  if ((n - idx - 1) / A[0] < 2)
    cout << "No" << endl, exit(0);
  cout << "Yes" << endl, exit(0);

  assert(0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 100001
using namespace std;
typedef pair<int, int> P;

int A[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  A[n] = 1e9;
  sort(A, A + n);

  if (A[0] == n - 1)
    cout << "Yes" << endl, exit(0);
  if (A[n - 1] - A[0] >= 2)
    cout << "No" << endl, exit(0);
  if (A[0] == A[n - 1]) {
    if (n / A[0] < 2)
      cout << "No" << endl, exit(0);
    cout << "Yes" << endl, exit(0);
  }

  int idx = 0;
  while (idx < n && A[idx] == A[idx + 1])
    idx++;
  if (idx > 0 && A[0] == idx + 1)
    cout << "Yes" << endl, exit(0);
  if (idx > 0 && A[0] < idx + 1)
    cout << "No" << endl, exit(0);
  if ((n - idx - 1) / (A[0] - idx) < 2)
    cout << "No" << endl, exit(0);
  cout << "Yes" << endl, exit(0);

  assert(0);
  return 0;
}
Problem p03688 (C++)

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

const int mnx = 5e3 + 9;
const int mod = 1e9 + 7;

ll n;
ll a[mnx];
ll mn = mnx, mx, cmx, cm1;

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mn = min(mn, a[i]);
    mx = max(mx, a[i]);
  }

  if (mx - mn <= 1) {
    for (int i = 0; i < n; i++) {
      cmx += (a[i] == mx);
      cm1 += (a[i] == mx - 1);
    }
    if (cmx == 1) {
      cout << "No" << '\n';
      return 0;
    }
    if (!cm1 && mx == n - 1) {
      cout << "Yes" << '\n';
      return 0;
    }
    ll l = cm1 + 1;
    ll r = cm1 + (n - cm1) / 2;
    if (mx >= l && mx <= r)
      cout << "Yes" << '\n';
    else
      cout << "No" << '\n';
  }

  else
    cout << "No" << '\n';

  return 0;
}


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

const int mnx = 1e6 + 9;
const int mod = 1e9 + 7;

ll n;
ll a[mnx];
ll mn = mnx, mx, cmx, cm1;

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mn = min(mn, a[i]);
    mx = max(mx, a[i]);
  }

  if (mx - mn <= 1) {
    for (int i = 0; i < n; i++) {
      cmx += (a[i] == mx);
      cm1 += (a[i] == mx - 1);
    }
    if (cmx == 1) {
      cout << "No" << '\n';
      return 0;
    }
    if (!cm1 && mx == n - 1) {
      cout << "Yes" << '\n';
      return 0;
    }
    ll l = cm1 + 1;
    ll r = cm1 + (n - cm1) / 2;
    if (mx >= l && mx <= r)
      cout << "Yes" << '\n';
    else
      cout << "No" << '\n';
  }

  else
    cout << "No" << '\n';

  return 0;
}
Problem p03688 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF LLONG_MAX
#define eps LDBL_EPSILON
#define moder 1000000007
#define pie 3.141592653589793238462643383279
#define P std::pair<int, int>
#define prique priority_queue
using namespace std;
int n, a[100010];
signed main() {
  cin >> n;
  int mi = INF, ma = 0;
  rep(i, n) {
    cin >> a[i];
    mi = min(mi, a[i]);
    ma = max(ma, a[i]);
  }
  if (ma - mi > 1) {
    puts("No");
    return 0;
  }
  int x = 0, y = 0;
  rep(i, n) {
    if (mi == a[i])
      x++;
    if (ma == a[i])
      y++;
  }
  if (ma != mi) {
    if (x == 1) {
      if (ma - 1 == n - 2)
        puts("Yes");
      else if (ma > 1 && (n - 1) / (ma - 1) >= 2)
        puts("Yes");
      else
        puts("No");
    } else if (y == 1) {
      if (mi - 1 == n - 2)
        puts("Yes");
      else if (mi > 1 && (n - 1) / (mi - 1) >= 2)
        puts("Yes");
      else
        puts("No");
    } else {
      if ((n - 1) / (ma - 1) >= 2 && (n - 1) / (mi - 1) >= 2)
        puts("Yes");
      else
        puts("No");
    }
  } else {
    if (mi == n - 1)
      puts("Yes");
    else if (n / mi >= 2)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF LLONG_MAX
#define eps LDBL_EPSILON
#define moder 1000000007
#define pie 3.141592653589793238462643383279
#define P std::pair<int, int>
#define prique priority_queue
using namespace std;
int n, a[100010];
signed main() {
  cin >> n;
  int mi = INF, ma = 0;
  rep(i, n) {
    cin >> a[i];
    mi = min(mi, a[i]);
    ma = max(ma, a[i]);
  }
  if (ma - mi > 1) {
    puts("No");
    return 0;
  }
  int x = 0, y = 0;
  rep(i, n) {
    if (mi == a[i])
      x++;
    if (ma == a[i])
      y++;
  }
  if (ma != mi) {
    if (x < ma && y / (ma - x) >= 2)
      puts("Yes");
    else
      puts("No");
  } else {
    if (mi == n - 1)
      puts("Yes");
    else if (n / mi >= 2)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}