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

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
ll dpL[5001][5001];
ll dpR[5001][5001];
int a[5001];
int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i] = min(a[i], K);
  }
  dpL[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      dpL[i + 1][j] |= dpL[i][j];
      dpL[i + 1][min(j + a[i], K)] |= dpL[i][j];
    }
  }
  dpR[N][0] = 1;
  for (int i = N; i > 0; i--) {
    for (int j = 0; j <= K; j++) {
      dpR[i - 1][j] |= dpR[i][j];
      dpR[i - 1][min(j + a[i - 1], K)] |= dpR[i][j];
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 1; j <= K; j++)
      dpR[i][j] += dpR[i][j - 1];
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    bool need = 0;
    for (int j = 0; j < K; j++) {
      if (!dpL[i][j])
        continue;
      int s = dpR[i + 1][K - 1 - j];
      if (K - a[i] - j > 0)
        s -= dpR[i + 1][K - a[i] - j - 1];
      if (s > 0) {
        need = 1;
        break;
      }
    }
    if (need)
      ans++;
  }
  cout << N - ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
bool dpL[5001][5001];
ll dpR[5001][5001];
int a[5001];
int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    a[i] = min(a[i], K);
  }
  dpL[0][0] = 1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      dpL[i + 1][j] |= dpL[i][j];
      dpL[i + 1][min(j + a[i], K)] |= dpL[i][j];
    }
  }
  dpR[N][0] = 1;
  for (int i = N; i > 0; i--) {
    for (int j = 0; j <= K; j++) {
      dpR[i - 1][j] |= dpR[i][j];
      dpR[i - 1][min(j + a[i - 1], K)] |= dpR[i][j];
    }
  }
  for (int i = 0; i <= N; i++) {
    for (int j = 1; j <= K; j++)
      dpR[i][j] += dpR[i][j - 1];
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    bool need = 0;
    for (int j = 0; j < K; j++) {
      if (!dpL[i][j])
        continue;
      int s = dpR[i + 1][K - 1 - j];
      if (K - a[i] - j > 0)
        s -= dpR[i + 1][K - a[i] - j - 1];
      if (s > 0) {
        need = 1;
        break;
      }
    }
    if (need)
      ans++;
  }
  cout << N - ans << endl;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define repn(i, n) for (int i = 1; i <= int(n); i++)
#define set(i, n) memset(i, n, sizeof(i))

#define pb push_back
#define f first
#define s second

using namespace std;

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

const int N = 505;
const int oo = 1e6;

int dp[N][N];
int n, a[N], k;

bool OK(int pos) {
  if (a[pos] >= k)
    return false;
  int val = a[pos];
  a[pos] = 0;
  int need = k - val;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= need; j++)
      dp[i][j] = oo;
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= need; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      int f = min(need, j + a[i]);
      dp[i][f] = min(dp[i][f], dp[i - 1][j] + a[i]);
    }
  }
  /*
  cout << "#" << val << endl;
  for(int i=1;i<=n;i++){
          for(int j=0;j<=need;j++){
                  printf("%7d", dp[i][j]);
          }
          printf("\n");
  }
  */
  bool ret = false;
  if (dp[n][need] >= k)
    ret = 1;
  a[pos] = val;
  return ret;
}

int main() {
  scanf("%d %d", &n, &k);
  repn(i, n) scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  int lo = 1, hi = n, ret = 0;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (OK(mid))
      ret = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  cout << ret << endl;
  return 0;
}


Fixed submission:
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define repn(i, n) for (int i = 1; i <= int(n); i++)
#define set(i, n) memset(i, n, sizeof(i))

#define pb push_back
#define f first
#define s second

using namespace std;

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

const int N = 5005;
const int oo = 1e6;

int dp[N][N];
int n, a[N], k;

bool OK(int pos) {
  if (a[pos] >= k)
    return false;
  int val = a[pos];
  a[pos] = 0;
  int need = k - val;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= need; j++)
      dp[i][j] = oo;
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= need; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      int f = min(need, j + a[i]);
      dp[i][f] = min(dp[i][f], dp[i - 1][j] + a[i]);
    }
  }
  /*
  cout << "#" << val << endl;
  for(int i=1;i<=n;i++){
          for(int j=0;j<=need;j++){
                  printf("%7d", dp[i][j]);
          }
          printf("\n");
  }
  */
  bool ret = false;
  if (dp[n][need] >= k)
    ret = 1;
  a[pos] = val;
  return ret;
}

int main() {
  scanf("%d %d", &n, &k);
  repn(i, n) scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  int lo = 1, hi = n, ret = 0;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (OK(mid))
      ret = mid, lo = mid + 1;
    else
      hi = mid - 1;
  }
  cout << ret << endl;
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define sz(c) ((int)(c).size())

using namespace std;
typedef long long ll;
typedef vector<int> vi;
// const int MAX=;

int N, K;
int a[5002];
bool dp[5002][5002];

bool nn(int i) {
  // i番目を使わず和をK-a[i]以上K-1以下にできるかどうか
  // dp[j][k]はj番目までで和をkにできるかどうか
  dp[0][0] = true;
  rep1(j, N) {
    if (j == i) {
      rep(k, K) dp[j][k] = dp[j - 1][k];
    } else {
      rep(k, K) {
        dp[j][k] = dp[j - 1][k];
        if (a[j] <= k)
          dp[j][k] = (dp[j][k] || dp[j - 1][k - a[j]]);
      }
    }
  }
  bool ok = false;
  for (int k = K - a[i]; k < K; k++)
    if (dp[N][k])
      ok = true;

  return !ok;
}

int main() {
  cin >> N >> K;
  rep1(i, N) cin >> a[i];
  a[0] = 0;
  sort(a, a + (N + 1));

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

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define sz(c) ((int)(c).size())

using namespace std;
typedef long long ll;
typedef vector<int> vi;
// const int MAX=;

int N, K;
int a[5002];
bool dp[5002][5002];

bool nn(int i) {
  // i番目を使わず和をK-a[i]以上K-1以下にできるかどうか
  // dp[j][k]はj番目までで和をkにできるかどうか
  dp[0][0] = true;
  rep1(j, N) {
    if (j == i) {
      rep(k, K) dp[j][k] = dp[j - 1][k];
    } else {
      rep(k, K) {
        dp[j][k] = dp[j - 1][k];
        if (a[j] <= k)
          dp[j][k] = (dp[j][k] || dp[j - 1][k - a[j]]);
      }
    }
  }
  bool ok = false;
  for (int k = max(0, K - a[i]); k < K; k++)
    if (dp[N][k])
      ok = true;

  return !ok;
}

int main() {
  cin >> N >> K;
  rep1(i, N) cin >> a[i];
  a[0] = 0;
  sort(a, a + (N + 1));

  int lb = 0, ub = N + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    if (nn(mid))
      lb = mid;
    else
      ub = mid;
  }
  cout << lb;
}
Problem p03782 (C++)

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

using namespace std;

int main() {
  int N, K;
  int a[5003];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a, a + N);
  bool dp[5003]; // [和] = is, count
  fill(dp, dp + 5003, false);
  dp[0] = true;
  int okindex = N;
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 5000; j >= 0; j--) {
      if (dp[j] && j + a[i] >= K) {
        okindex = min((int)(lower_bound(a, a + N, K - j) - a), okindex);
      } else {
        dp[j + a[i]] |= dp[j];
      }
    }
  }
  cout << okindex << endl;

  return 0;
}

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

using namespace std;

int main() {
  int N, K;
  int a[5003];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a, a + N);
  bool dp[5003]; // [和] = is, count
  fill(dp, dp + 5003, false);
  dp[0] = true;
  int okindex = N;
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 5000; j >= 0; j--) {
      if (not dp[j])
        continue;
      if (j + a[i] >= K) {
        okindex = min((int)(lower_bound(a, a + N, K - j) - a), okindex);
      } else {
        dp[j + a[i]] |= dp[j];
      }
    }
  }
  cout << okindex << endl;

  return 0;
}
Problem p03783 (C++)

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

typedef long long ll;
const ll INF = 10000000000000000;

int main() {
  int n;
  int l[402], r[402];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
  }
  l[0] = r[0] = 0;

  priority_queue<ll> L;
  priority_queue<ll, vector<ll>, greater<ll>> R;
  ll dL = 0, dR = 0;
  ll m = 0;
  L.push(-INF);
  R.push(INF);
  for (int i = 1; i <= n; i++) {
    dR += r[i - 1] - l[i - 1];
    dL -= r[i] - l[i];
    ll x = L.top() + dL;
    ll y = R.top() + dR;
    if (l[i] < x) {
      L.push(l[i] - dL);
      L.push(l[i] - dL);
      L.pop();
      R.push(x - dR);
      m += x - l[i];
    } else if (l[i] > y) {
      R.push(l[i] - dR);
      R.push(l[i] - dR);
      R.pop();
      L.push(y - dL);
      m += l[i] - y;
    } else {
      L.push(l[i] - dL);
      R.push(l[i] - dR);
    }
  }
  cout << m << endl;
}

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

typedef long long ll;
const ll INF = 10000000000000000;

int main() {
  static int n;
  static int l[100010], r[100010];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
  }
  l[0] = r[0] = 0;

  priority_queue<ll> L;
  priority_queue<ll, vector<ll>, greater<ll>> R;
  ll dL = 0, dR = 0;
  ll m = 0;
  L.push(-INF);
  R.push(INF);
  for (int i = 1; i <= n; i++) {
    dR += r[i - 1] - l[i - 1];
    dL -= r[i] - l[i];
    ll x = L.top() + dL;
    ll y = R.top() + dR;
    if (l[i] < x) {
      L.push(l[i] - dL);
      L.push(l[i] - dL);
      L.pop();
      R.push(x - dR);
      m += x - l[i];
    } else if (l[i] > y) {
      R.push(l[i] - dR);
      R.push(l[i] - dR);
      R.pop();
      L.push(y - dL);
      m += l[i] - y;
    } else {
      L.push(l[i] - dL);
      R.push(l[i] - dR);
    }
  }
  cout << m << endl;
}
Problem p03783 (C++)

Buggy submission (status: Time Limit Exceeded):
// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x)                                                                \
  freopen(#x ".in", "r", stdin);                                               \
  freopen(#x ".out", "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;

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

const int N = 100000 + 10;

int n, l[N], r[N];
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll>> R;

int main() {
  file(road);
  n = read();
  for (int i = 1; i <= n; ++i)
    l[i] = read(), r[i] = read();
  L.push(l[1]), R.push(l[1]);
  ll addl = 0, addr = 0, ans = 0;
  for (int i = 2; i <= n; ++i) {
    addl -= r[i] - l[i], addr += r[i - 1] - l[i - 1];
    ll lb = L.top() + addl, rb = R.top() + addr;
    if (lb <= l[i] && l[i] <= rb)
      L.push(l[i] - addl), R.push(l[i] - addr);
    else if (l[i] < lb) {
      ans += lb - l[i];
      R.push(lb - addr);
      L.pop(), L.push(l[i] - addl), L.push(l[i] - addl);
    } else {
      ans += l[i] - rb;
      L.push(rb - addl);
      R.pop(), R.push(l[i] - addr), R.push(l[i] - addr);
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x)                                                                \
  freopen(#x ".in", "r", stdin);                                               \
  freopen(#x ".out", "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;

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

const int N = 100000 + 10;

int n, l[N], r[N];
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll>> R;

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    l[i] = read(), r[i] = read();
  L.push(l[1]), R.push(l[1]);
  ll addl = 0, addr = 0, ans = 0;
  for (int i = 2; i <= n; ++i) {
    addl -= r[i] - l[i], addr += r[i - 1] - l[i - 1];
    ll lb = L.top() + addl, rb = R.top() + addr;
    if (lb <= l[i] && l[i] <= rb)
      L.push(l[i] - addl), R.push(l[i] - addr);
    else if (l[i] < lb) {
      ans += lb - l[i];
      R.push(lb - addr);
      L.pop(), L.push(l[i] - addl), L.push(l[i] - addl);
    } else {
      ans += l[i] - rb;
      L.push(rb - addl);
      R.pop(), R.push(l[i] - addr), R.push(l[i] - addr);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03785 (C++)

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

// 関数

// main関数
int main() {

  // 入力
  long N, C, K;
  cin >> N >> C >> K;
  vector<long> T(N);
  for (int i = 0; i < N; i++) {
    cin >> T.at(i);
  }

  // 計算
  sort(T.begin(), T.end());

  /*for(int i=0;i<N;i++){
      cout<<T.at(i)<<endl;
  }*/

  long ans = 0;
  while (T.size() > 0) {
    ans++;
    for (int i = C - 1; i >= 0; i--) {
      if (i < T.size() && T.at(i) <= T.at(0) + K) {
        T.erase(T.begin() + i);
      }
    }
  }

  // 出力
  cout << ans << endl;
}

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

// 関数

// main関数
int main() {

  // 入力
  long N, C, K;
  cin >> N >> C >> K;
  vector<long> T(N);
  for (int i = 0; i < N; i++) {
    cin >> T.at(i);
  }

  // 計算
  sort(T.begin(), T.end());

  /*for(int i=0;i<N;i++){
      cout<<T.at(i)<<endl;
  }*/

  long ans = 0;
  while (T.size() > 0) {
    ans++;
    for (int i = C - 1; i >= 0; i--) {
      if (i < T.size() && T.at(i) <= T.at(0) + K) {
        T.erase(T.begin(), T.begin() + i + 1);
        break;
      }
    }
  }

  // 出力
  cout << ans << endl;
}
Problem p03785 (Python)

Buggy submission (status: Runtime Error):
N, C, K = [int(i) for i in input().split()]
T = sorted([int(input())] for i in range(N))
ans = 1
limit = 0
ti = T[0]
for i in range(N - 1):
    limit += 1
    if ti + K < T[i + 1] or limit == C:
        ti = T[i + 1]
        ans += 1
        limit = 0
print(ans)


Fixed submission:
N, C, K = [int(i) for i in input().split()]
T = sorted([int(input()) for i in range(N)])
ans = 1
limit = 0
ti = T[0]
for i in range(N - 1):
    limit += 1
    if ti + K < T[i + 1] or limit == C:
        ti = T[i + 1]
        ans += 1
        limit = 0
print(ans)
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (1 << 29)
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int p) {
  for (int i = 2; i <= sqrt(p); i++) {
    if (!p % i)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % MOD;
  if (b % 2)
    return modpow(a, b - 1) * a % MOD;
  else
    return modpow(a, b / 2) * modpow(a, b / 2) % MOD;
}
int comb(int a, int b) {
  if (b == 0 || a == b)
    return 1;
  if (b == 1)
    return a % MOD;
  return (comb(a - 1, b - 1) + comb(a - 1, b)) % MOD;
}
// Library End !
int n, c, k, t[114514], h = 0, i = 0, ans = 0;
signed main() {
  cin >> n >> c >> k;
  rep(i, n) cin >> t[i];
  sort(t, t + n);
  while (i < n) {
    while (i - h + 1 <= c && t[h] + k >= t[i])
      i++;
    h = i;
    ans++;
    if (i == n - 1) {
      cout << ans + 1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (1 << 29)
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int p) {
  for (int i = 2; i <= sqrt(p); i++) {
    if (!p % i)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % MOD;
  if (b % 2)
    return modpow(a, b - 1) * a % MOD;
  else
    return modpow(a, b / 2) * modpow(a, b / 2) % MOD;
}
int comb(int a, int b) {
  if (b == 0 || a == b)
    return 1;
  if (b == 1)
    return a % MOD;
  return (comb(a - 1, b - 1) + comb(a - 1, b)) % MOD;
}
// Library End !
int n, c, k, t[114514], h = 0, i = 0, ans = 0;
signed main() {
  cin >> n >> c >> k;
  rep(i, n) cin >> t[i];
  sort(t, t + n);
  while (i < n) {
    while (i - h + 1 <= c && t[i] <= t[h] + k && i < n)
      i++;
    h = i;
    ans++;
    if (i == n - 1) {
      cout << ans + 1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <sstream>
// STL
#include <algorithm>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// C++11 needed
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;

int N, C, K, T[100003];

int main() {
  scanf("%d %d %d", &N, &C, &K);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &T[i]);
  sort(T + 1, T + N + 1);
  int pos = 1, num = 0;
  while (pos <= N) {
    int p = pos;
    ++num;
    while (p - pos < C && T[p] - T[pos] <= K)
      ++p;
    pos = p;
  }
  printf("%d", num);
  return 0;
}


Fixed submission:
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <sstream>
// STL
#include <algorithm>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// C++11 needed
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;

int N, C, K, T[100003];

int main() {
  scanf("%d %d %d", &N, &C, &K);
  for (int i = 1; i <= N; ++i)
    scanf("%d", &T[i]);
  sort(T + 1, T + N + 1);
  int pos = 1, num = 0;
  while (pos <= N) {
    int p = pos;
    ++num;
    while (p <= N && p - pos < C && T[p] - T[pos] <= K)
      ++p;
    pos = p;
  }
  printf("%d", num);
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

int main() {
  ll N, C, K;
  cin >> N >> C >> K;
  vector<ll> T(N);
  rep(i, N) { cin >> T.at(i); }
  ll bus = 0;
  sort(T.begin(), T.end());
  for (ll p = 0; p < N; p++) {
    ll q = p;
    while (T.at(q) <= T.at(p) + K) {
      q++;
      if (q - p == C) {
        break;
      }
    }
    bus++;
    p = q - 1;
  }
  cout << bus << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

int main() {
  ll N, C, K;
  cin >> N >> C >> K;
  vector<ll> T(N);
  rep(i, N) { cin >> T.at(i); }
  ll bus = 0;
  sort(T.begin(), T.end());
  for (ll p = 0; p < N; p++) {
    ll q = p;
    while (T.at(q) <= T.at(p) + K) {
      q++;
      if (q - p == C) {
        break;
      }
      if (q == N) {
        break;
      }
    }
    bus++;
    p = q - 1;
  }
  cout << bus << endl;
}
Problem p03785 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n, c, k, t;
  priority_queue<int, vector<int>, greater<int>> q;
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++) {
    cin >> t;
    q.push(t);
  }

  long long lim = -1, bn = 0, bl = c;
  while (!q.empty()) {
    if (bl == 0) {
      lim = q.top() + k;
      q.pop();
      bl = c - 1;
      bn += 1;
    } else {
      if (q.top() <= lim) {
        q.pop();
        bl -= 1;
        // if (bl==0) {bl=k;}
      } else {
        bl = k;
      }
    }
  }
  cout << bn << endl;
  return 0;
}

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

int main() {
  int n, c, k, t;
  priority_queue<int, vector<int>, greater<int>> q;
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++) {
    cin >> t;
    q.push(t);
  }

  long long lim = -1, bn = 0, bl = c;
  while (!q.empty()) {
    if (bl == 0) {
      lim = q.top() + k;
      q.pop();
      bl = c - 1;
      bn += 1;
    } else {
      if (q.top() <= lim) {
        q.pop();
        bl -= 1;
        // if (bl==0) {bl=k;}
      } else {
        bl = 0;
      }
    }
  }
  cout << bn << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int count = 0, n, c, k, in, t;
  vector<int> v;
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++) {
    cin >> in;
    v.push_back(in);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    count++;
    t = v[i] + k;
    for (in = 0; in < c - 1; in++) {
      if (v[i + in + 1] > t)
        break;
    }
    i += in;
  }
  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int count = 0, n, c, k, in, t;
  vector<int> v;
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++) {
    cin >> in;
    v.push_back(in);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < n; i++) {
    count++;
    t = v[i] + k;
    for (in = 0; in < c - 1 && (i + in + 1) < n; in++) {
      if (v[i + in + 1] > t)
        break;
    }
    i += in;
  }
  cout << count << endl;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;
#define N (int)1e5
#define INF (int)1e9
#define mod (int)1e9 + 7
#define LL long long
int n, c, k, ans, i2;
int t[N];
int main() {
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++)
    cin >> t[i];
  sort(t, t + n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1;; j++) {
      if (t[j] > t[i] + k) {
        i = j - 1;
        ans++;
        break;
      }
      if (j == i + c - 1 || j == n - 1) {
        i = j;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;
#define N (int)1e5
#define INF (int)1e9
#define mod (int)1e9 + 7
#define LL long long
int n, c, k, ans, i2;
int t[N];
int main() {
  cin >> n >> c >> k;
  for (int i = 0; i < n; i++)
    cin >> t[i];
  sort(t, t + n);
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (t[j] > t[i] + k) {
        i = j - 1;
        ans++;
        break;
      }
      if (j == i + c - 1 || j == n - 1) {
        i = j;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p03785 (C++)

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

using namespace std;

const int N = 10010;

int n;
long long c, k, t[N];

int main(int argc, char const *argv[]) {
  scanf("%d %lld %lld", &n, &c, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", t + i);
  }
  sort(t + 1, t + n + 1);
  int bus = 0;
  for (int i = 1; i <= n;) {
    ++bus;
    long long now = t[i];
    long long fin = now + k;
    int pos = upper_bound(t + 1, t + n + 1, fin) - t;
    int next = min(i + int(c), pos);
    i = next;
  }
  printf("%d\n", bus);
  return 0;
}


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

using namespace std;

const int N = 100010;

int n;
long long c, k, t[N];

int main(int argc, char const *argv[]) {
  scanf("%d %lld %lld", &n, &c, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", t + i);
  }
  sort(t + 1, t + n + 1);
  int bus = 0;
  for (int i = 1; i <= n;) {
    ++bus;
    long long now = t[i];
    long long fin = now + k;
    int pos = upper_bound(t + 1, t + n + 1, fin) - t;
    int next = min(i + int(c), pos);
    i = next;
  }
  printf("%d\n", bus);
  return 0;
}
Problem p03785 (C++)

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

using namespace std;

using ll = long long;
using vi = vector<int>;

const ll linf = 1LL << 62;
const int inf = 999999;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll atcoder = int(1e9 + 7);

#define Endl endl
#define pr(x) cout << x << endl
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(x) x.begin(), x.end()
#define atcoder(int) 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)

ll gcd(ll a, ll b) { // 最大公約数
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}

ll lcm(ll a, ll b) { // 最小公倍数
  return (a / gcd(a, b)) * b;
}

ll t[100010];

int main() {

  int n, c, k;
  cin >> n >> c >> k;
  rep(i, n) cin >> t[i];
  sort(t, t + n);
  int ans = 0;

  rep(i, n) {
    int a = t[i];
    i++;
    ans++;
    if (i > n)
      break;
    for (int j = 0; j < c - 1; j++) {
      if (t[i] <= a + k)
        i++;
      else
        break;
    }
    i--;
  }

  pr(ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using vi = vector<int>;

const ll linf = 1LL << 62;
const int inf = 999999;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll atcoder = int(1e9 + 7);

#define Endl endl
#define pr(x) cout << x << endl
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(x) x.begin(), x.end()
#define atcoder(int) 1e9 + 7
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)

ll gcd(ll a, ll b) { // 最大公約数
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}

ll lcm(ll a, ll b) { // 最小公倍数
  return (a / gcd(a, b)) * b;
}

ll t[100010];

int main() {

  int n, c, k;
  cin >> n >> c >> k;
  rep(i, n) cin >> t[i];
  sort(t, t + n);
  int ans = 0;

  rep(i, n) {
    int a = t[i];
    i++;
    ans++;
    if (i > n)
      break;
    for (int j = 0; j < c - 1; j++) {
      if (i > n)
        break;
      if (t[i] <= a + k)
        i++;
      else
        break;
    }
    i--;
  }

  pr(ans);
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int main() {
  int n, c, k, ans, i, j;
  scanf("%d%d%d", &n, &c, &k);
  for (i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n);
  for (ans = i = 0; i < n; ans++) {
    for (j = 0; j < c; j++)
      if (a[i + j] - a[i] > k)
        break;
    i += j;
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int main() {
  int n, c, k, ans, i, j;
  scanf("%d%d%d", &n, &c, &k);
  for (i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n);
  for (ans = i = 0; i < n; ans++) {
    for (j = 0; i + j < n && j < c; j++)
      if (a[i + j] - a[i] > k)
        break;
    i += j;
  }
  printf("%d", ans);
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, C, K, S = 0;
  cin >> N >> C >> K;
  vector<int> T(N);
  for (int i = 0; i < N; i++)
    cin >> T.at(i);
  sort(T.begin(), T.end());
  for (int i = 0; i < N; i++) {
    S++;
    int j = 1;
    while (j < C && T.at(i) + C >= T.at(i + j))
      j++;
    i += j - 1;
  }
  cout << S << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, C, K, S = 0;
  cin >> N >> C >> K;
  vector<int> T(N);
  for (int i = 0; i < N; i++)
    cin >> T.at(i);
  sort(T.begin(), T.end());
  for (int i = 0; i < N; i++) {
    S++;
    int j = 1;
    while (i + j < N && j < C && T.at(i) + K >= T.at(i + j))
      j++;
    i += j - 1;
  }
  cout << S << endl;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#define cle(a) memset(a, 0, sizeof(a))
#define maxn 1000005

using namespace std;
typedef long long ll;
// n个人  每辆车 c个人  超过k分钟生气
int n, c, k;
int a[100005];
int main() {
  while (cin >> n >> c >> k) {
    memset(a, 0, sizeof(a));
    for (int i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    int flag = 0; // 表示车

    for (int i = 0; i < n; i++) {
      // cout<<"adjfklasjdf"<<i<<endl;
      int temp = 1;
      int j;
      for (j = i + 1;;) {
        if (a[j] > a[i] + k) {
          j--;
          break;
        } else
          temp++;
        if (temp > c) {
          j--;
          break;
        }
        j++;
      }
      i = j;
      flag++;
    }
    cout << flag << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#define cle(a) memset(a, 0, sizeof(a))
#define maxn 1000005

using namespace std;
typedef long long ll;
// n个人  每辆车 c个人  超过k分钟生气
int n, c, k;
int a[100005];
int main() {
  while (cin >> n >> c >> k) {
    memset(a, 0, sizeof(a));
    for (int i = 0; i < n; i++)
      cin >> a[i];
    sort(a, a + n);
    int flag = 0; // 表示车

    for (int i = 0; i < n; i++) {
      // cout<<"adjfklasjdf"<<i<<endl;
      int temp = 1;
      int j;
      for (j = i + 1;;) {
        if (a[j] > a[i] + k) {
          j--;
          break;
        } else
          temp++;
        if (temp > c) {
          j--;
          break;
        }
        if (j >= n)
          break;
        j++;
      }
      i = j;
      flag++;
    }
    cout << flag << endl;
  }

  return 0;
}
Problem p03785 (C++)

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

int main() {
  int N, C, K;
  cin >> N >> C >> K;
  long T[100000];
  for (int i = 0; i < N; i++) {
    cin >> T[i];
  }
  for (int i = N; i < N + C; i++) {
    T[i] = 1000000000 + K + 100;
  }
  sort(T, T + N);

  /*for (int i = 0; i < N + C; i++){
      cout << T[i] << endl;
  }*/

  int count = 0;
  int buscount = 0;
  for (int i = 0; i < N; i++) {
    count = 0;
    for (int j = 1; j < C; j++) {
      if ((T[i] + K) >= T[i + j] && T[i] + K <= T[i + j] + K) {
        count++;
      }
    }
    // cout <<"i" <<  i << endl;
    // cout << "count" << count << endl;
    i += count;
    buscount++;
  }
  cout << buscount << endl;
}

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

int main() {
  int N, C, K;
  cin >> N >> C >> K;
  long T[1000000];
  for (int i = 0; i < N; i++) {
    cin >> T[i];
  }
  for (int i = N; i < N + C; i++) {
    T[i] = 1000000000 + K + 100;
  }
  sort(T, T + N);

  /*for (int i = 0; i < N + C; i++){
      cout << T[i] << endl;
  }*/

  int count = 0;
  int buscount = 0;
  for (int i = 0; i < N; i++) {
    count = 0;
    for (int j = 1; j < C; j++) {
      if ((T[i] + K) >= T[i + j] && T[i] + K <= T[i + j] + K) {
        count++;
      }
    }
    // cout <<"i" <<  i << endl;
    // cout << "count" << count << endl;
    i += count;
    buscount++;
  }
  cout << buscount << endl;
}
Problem p03785 (C++)

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

int main() {
  int n, c, k;
  cin >> n >> c >> k;
  vector<LL> t(n);
  for (int i = 0; i < n; i++)
    cin >> t[i];
  sort(t.begin(), t.end());
  int ans = 0;
  int cur = 0;
  while (cur < n) {
    int b = 0;
    while (b + 1 < n && t[b + 1] <= t[cur] + k && b + 1 < cur + c)
      b++;
    ans++;
    cur = b + 1;
  }
  cout << ans << '\n';
}

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

int main() {
  int n, c, k;
  cin >> n >> c >> k;
  vector<LL> t(n);
  for (int i = 0; i < n; i++)
    cin >> t[i];
  sort(t.begin(), t.end());
  int ans = 0;
  int cur = 0;
  while (cur < n) {
    int b = cur;
    while (b + 1 < n && t[b + 1] <= t[cur] + k && b + 1 < cur + c)
      b++;
    ans++;
    cur = b + 1;
  }
  cout << ans << '\n';
}
Problem p03785 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

int main() {
  ll N, C, K;
  cin >> N >> C >> K;
  vector<ll> T(N);
  REP(i, N) cin >> T[i];
  sort(T.begin(), T.end());

  ll res = 0;
  ll i = 0;
  ll j = 0;
  while (1) {
    // dump(i)
    // dump(j)
    while (T[j + 1] <= T[i] + K && (j + 1) - i + 1 <= C) {
      j++;
      if (j == N - 1)
        break;
    }
    res++;
    if (j == N - 1) {
      break;
    }
    i = j + 1;
    j = i;
  }
  cout << res << endl;
  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

int main() {
  ll N, C, K;
  cin >> N >> C >> K;
  vector<ll> T(N);
  REP(i, N) cin >> T[i];
  sort(T.begin(), T.end());

  ll res = 0;
  ll i = 0;
  ll j = 0;
  while (1) {
    // dump(i)
    // dump(j)
    if (i == N - 1) {
      res++;
      break;
    }
    while (T[j + 1] <= T[i] + K && (j + 1) - i + 1 <= C) {
      j++;
      if (j == N - 1)
        break;
    }
    res++;
    if (j == N - 1) {
      break;
    }
    i = j + 1;
    j = i;
  }
  cout << res << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
// typedef pair<double, double> P;
const int dx[9] = {-1, 0, 0, 1, -1, -1, 1, 1, 0};
const int dy[9] = {0, -1, 1, 0, -1, 1, -1, 1, 0};

int N, C, K;
int T[101010];

signed main() {
  cin >> N >> C >> K;
  REP(i, N) cin >> T[i];
  sort(T, T + N);

  int ans = 0;
  int cur = 0;
  int num = C;

  int pos = T[0] + K;
  ans++;
  cur++;
  num--;

  while (cur < N) {
    while (0 < num && T[cur] <= pos) {
      num--;
      cur++;
    }
    pos = T[cur] + K;
    // cout << "# " << cur << endl;
    if (cur < N) {
      ans++;
    }
    cur++;
    num = C - 1;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
// typedef pair<double, double> P;
const int dx[9] = {-1, 0, 0, 1, -1, -1, 1, 1, 0};
const int dy[9] = {0, -1, 1, 0, -1, 1, -1, 1, 0};

int N, C, K;
int T[101010];

signed main() {
  cin >> N >> C >> K;
  REP(i, N) cin >> T[i];
  sort(T, T + N);

  int ans = 0;
  int cur = 0;
  int num = C;

  int pos = T[0] + K;
  ans++;
  cur++;
  num--;

  while (cur < N) {
    while (0 < num && cur < N && T[cur] <= pos) {
      num--;
      cur++;
    }
    pos = T[cur] + K;
    // cout << "# " << cur << endl;
    if (cur < N) {
      ans++;
    }
    cur++;
    num = C - 1;
  }

  cout << ans << endl;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int N = 110, P = 1e9 + 7;
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, c, k, a[N], ans;
int i, j;
int main() {
  // freopen("1.out","w",stdout);
  n = read();
  c = read();
  k = read();
  for (i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i = j) {
    for (j = i; j <= n && j - i + 1 <= c && a[j] <= a[i] + k; j++)
      ;
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int N = 100100, P = 1e9 + 7;
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, c, k, a[N], ans;
int i, j;
int main() {
  // freopen("1.out","w",stdout);
  n = read();
  c = read();
  k = read();
  for (i = 1; i <= n; i++)
    a[i] = read();
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i = j) {
    for (j = i; j <= n && j - i + 1 <= c && a[j] <= a[i] + k; j++)
      ;
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, c, k, now = 0, count = 0;
  cin >> n >> c >> k;
  long long int t[n];
  for (int i = 0; i < n; i++)
    cin >> t[i];
  sort(t, t + n);
  while (now < n) {
    int fuya = 0;
    for (int i = now; i < now + c; i++) {
      if (t[now] + k >= t[i])
        fuya++;
    }
    count++;
    now += fuya;
  }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, c, k, now = 0, count = 0;
  cin >> n >> c >> k;
  long long int t[n];
  for (int i = 0; i < n; i++)
    cin >> t[i];
  sort(t, t + n);
  while (now < n) {
    int fuya = 0;
    for (int i = now; i < min(now + c, n); i++) {
      if (t[now] + k >= t[i])
        fuya++;
    }
    count++;
    now += fuya;
  }
  cout << count << endl;
}
Problem p03785 (C++)

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

// ----------- define --------------
#define int long long
#define vi vector<int>
#define vc vector<char>
#define ii pair<int, int>
#define fi first
#define sc second
#define all(x) (x).begin(), (x).end()
#define get_bit(x, k) ((x >> k) & 1)
// ---------------------------------

void MAIN() {
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  sort(all(a));
  int i = 0, ans = 0;
  while (i < n) {
    i = upper_bound(a.begin() + i, a.begin() + i + c, a[i] + k) - a.begin();
    ans++;
  }
  cout << ans << '\n';
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    MAIN();
}


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

// ----------- define --------------
#define int long long
#define vi vector<int>
#define vc vector<char>
#define ii pair<int, int>
#define fi first
#define sc second
#define all(x) (x).begin(), (x).end()
#define get_bit(x, k) ((x >> k) & 1)
// ---------------------------------

void MAIN() {
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  sort(all(a));
  int i = 0, ans = 0;
  while (i < n) {
    i = upper_bound(a.begin() + i, a.begin() + min(i + c, n), a[i] + k) -
        a.begin();
    ans++;
  }
  cout << ans << '\n';
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    MAIN();
}
Problem p03785 (C++)

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

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> t(n);
  rep(i, n) cin >> t[i];
  sort(t.begin(), t.end());
  ll cnt = 0;
  int i = 0;
  while (i < n) {
    int limit_t = t[i] + k;
    int tmp_cnt = 0;
    reps(j, c + 1, 1) {
      tmp_cnt++;
      if (t[i + j] >= limit_t) {
        break;
      }
    }
    if (tmp_cnt > 1)
      i += tmp_cnt;
    else
      i++;
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}

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

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, c, k;
  cin >> n >> c >> k;
  vector<int> t(n);
  rep(i, n) cin >> t[i];
  sort(t.begin(), t.end());
  ll cnt = 0;
  int i = 0;
  while (i < n) {
    int limit_t = t[i] + k;
    int tmp_cnt = 0;
    reps(j, c + 1, 1) {
      tmp_cnt++;
      if (i + j < n && t[i + j] > limit_t) {
        break;
      }
    }
    if (tmp_cnt > 1)
      i += tmp_cnt;
    else
      i++;
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p03785 (C++)

Buggy submission (status: Runtime Error):
// AGC 011 A
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define MAX 100000

int main() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> a;
  for (int i = 0; i < n; i++) {
    ll v;
    cin >> v;
    a.push_back(v);
  }
  sort(a.begin(), a.end());
  int ans = 0;
  for (int i = 0; i < n;) {
    ll key = a[i] + k;
    ll p = c;
    while (p >= 0 && key >= a[i]) {
      p--;
      i++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// AGC 011 A
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define MAX 100000

int main() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> a;
  for (int i = 0; i < n; i++) {
    ll v;
    cin >> v;
    a.push_back(v);
  }
  sort(a.begin(), a.end());
  int ans = 0;
  for (int i = 0; i < n;) {
    ll key = a[i] + k;
    ll p = c;
    while (p > 0 && key >= a[i] && i < n) {
      p--;
      i++;
    }
    ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03785 (C++)

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

using namespace std;

long long a[100100];

int main() {
  long long n, c, k;
  scanf("%lld%lld%lld", &n, &c, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + 1 + n);
  long long tmp = 1, ans = 0;
  while (tmp <= n) {
    long long be = a[tmp], en = a[tmp];
    for (int i = 1; i <= c; i++) {
      en = a[tmp];
      tmp++;
      if (en - be > k) {
        tmp--;
        break;
      }
    }
    ans++;
  }
  printf("%d\n", ans);
}

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

using namespace std;

long long a[100100];

int main() {
  long long n, c, k;
  scanf("%lld%lld%lld", &n, &c, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + 1 + n);
  long long tmp = 1, ans = 0;
  while (tmp <= n) {
    long long be = a[tmp], en = a[tmp];
    for (int i = 1; i <= c; i++) {
      en = a[tmp];
      if (tmp > n)
        break;
      tmp++;
      if (en - be > k) {
        tmp--;
        break;
      }
    }
    ans++;
  }
  printf("%d\n", ans);
}