Problem p02890 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LLI long long int
#define FOR(v, a, b) for (LLI v = (a); v < (b); ++v)
#define FORE(v, a, b) for (LLI v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for (LLI v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c, x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define popcount __builtin_popcount
#define UNIQ(v) (v).erase(unique(ALL(v)), (v).end())
#define bit(i) (1LL << (i))

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(...) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n) { return m / gcd(m, n) * n; }

template <typename I> void join(ostream &ost, I s, I t, string d = " ") {
  for (auto i = s; i != t; ++i) {
    if (i != s)
      ost << d;
    ost << *i;
  }
  ost << endl;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &a : v)
    is >> a;
  return is;
}

template <typename T, typename U> bool chmin(T &a, const U &b) {
  return (a > b ? a = b, true : false);
}
template <typename T, typename U> bool chmax(T &a, const U &b) {
  return (a < b ? a = b, true : false);
}
template <typename T, size_t N, typename U>
void fill_array(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

struct Init {
  Init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} init;

template <typename T, typename Container = vector<T>>
vector<pair<T, int64_t>> run_length_encode(const Container &v) {
  vector<pair<T, int64_t>> ret;

  for (auto &x : v) {
    if (ret.empty())
      ret.push_back({x, 1});
    else if (ret.back().fst == x)
      ++ret.back().snd;
    else
      ret.push_back({x, 1});
  }

  return ret;
}

int main() {
  int N;
  while (cin >> N) {
    vector<LLI> A(N);
    cin >> A;
    REP(i, N) A[i] -= 1;

    sort(ALL(A));

    vector<int> count(N);

    REP(i, N) { count[A[i]] += 1; }

    sort(RALL(count));
    while (count.back() == 0) {
      count.pop_back();
    }

    dump(count);

    auto count2 = run_length_encode<int>(count);

    dump(count2);

    FORE(k, 1, N) {

      auto check = [&](int x) {
        LLI s = 0;

        for (auto &p : count2) {
          s += min(x, p.fst) * p.snd;
        }

        return s >= (LLI)x * k;
      };

      int lb = -1, ub = N + 1;
      while (abs(lb - ub) > 1) {
        int mid = (lb + ub) / 2;

        if (check(mid)) {
          lb = mid;
        } else {
          ub = mid;
        }
      }

      cout << lb << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LLI long long int
#define FOR(v, a, b) for (LLI v = (a); v < (b); ++v)
#define FORE(v, a, b) for (LLI v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for (LLI v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for (auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for (auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c, x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define popcount __builtin_popcount
#define UNIQ(v) (v).erase(unique(ALL(v)), (v).end())
#define bit(i) (1LL << (i))

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(...) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n) { return m / gcd(m, n) * n; }

template <typename I> void join(ostream &ost, I s, I t, string d = " ") {
  for (auto i = s; i != t; ++i) {
    if (i != s)
      ost << d;
    ost << *i;
  }
  ost << endl;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &a : v)
    is >> a;
  return is;
}

template <typename T, typename U> bool chmin(T &a, const U &b) {
  return (a > b ? a = b, true : false);
}
template <typename T, typename U> bool chmax(T &a, const U &b) {
  return (a < b ? a = b, true : false);
}
template <typename T, size_t N, typename U>
void fill_array(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

struct Init {
  Init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} init;

template <typename T, typename Container = vector<T>>
vector<pair<T, int64_t>> run_length_encode(const Container &v) {
  vector<pair<T, int64_t>> ret;

  for (auto &x : v) {
    if (ret.empty())
      ret.push_back({x, 1});
    else if (ret.back().fst == x)
      ++ret.back().snd;
    else
      ret.push_back({x, 1});
  }

  return ret;
}

int main() {
  int N;
  while (cin >> N) {
    vector<LLI> A(N);
    cin >> A;
    REP(i, N) A[i] -= 1;

    sort(ALL(A));

    vector<int> count(N);

    REP(i, N) { count[A[i]] += 1; }

    sort(RALL(count));
    while (count.back() == 0) {
      count.pop_back();
    }

    dump(count);

    auto count2 = run_length_encode<int>(count);

    dump(count2);

    FORE(k, 1, N) {

      auto check = [&](int x) {
        LLI s = 0;

        for (auto &p : count2) {
          s += min(x, p.fst) * p.snd;
        }

        return s >= (LLI)x * k;
      };

      int lb = -1, ub = N / k + 1;
      while (abs(lb - ub) > 1) {
        int mid = (lb + ub) / 2;

        if (check(mid)) {
          lb = mid;
        } else {
          ub = mid;
        }
      }

      cout << lb << endl;
    }
  }

  return 0;
}