Tifa's CP Library

:heavy_check_mark: src/test_cpverifier/aizu/alds1_15_d.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_15_D"

#include "../../code/edh/huffman_tree.hpp"

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  strn s;
  std::cin >> s;
  std::map<char, u32> mp;
  for (char c : s) ++mp[c];
  vec<char> dict;
  vecu w;
  for (auto [k, v] : mp) {
    dict.push_back(k);
    w.push_back(v);
  }
  if (w.size() < 3) {
    if (w.size() == 1)
      std::cout << w[0] << "\n";
    else
      std::cout << w[0] + w[1] << "\n";
    return 0;
  }
  tifa_libs::huffman<u32> hf(w);
  auto x = hf.encode();
  u64 ans = 0;
  for (u32 i = 0; i < w.size(); ++i) ans += w[i] * x[i].size();
  std::cout << ans << '\n';
  return 0;
}
#line 1 "src/test_cpverifier/aizu/alds1_15_d.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/all/ALDS1_15_D"

#line 1 "src/code/edh/huffman_tree.hpp"



#line 1 "src/code/ds/radix_heap.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/ds/radix_heap.hpp"

namespace tifa_libs::ds {

template <std::unsigned_integral K, class V, class C = std::less<K>>
class radix_heap {
  static constexpr u32 B = sizeof(K) * 8;
  static constexpr C comp{};
  arr<vecp<K, V>, B + 1> vs;
  arr<K, B + 1> ms;
  u32 s;
  K last;

 public:
  constexpr explicit radix_heap() : s(0), last(0) { std::ranges::fill(ms, K(-1)); }

  constexpr u32 size() const { return s; }
  constexpr bool empty() const { return !s; }

  constexpr void emplace(K key, V const &val) {
    ++s;
    K b = (K)std::bit_width(key ^ last);
    vs[b].emplace_back(key, val), ms[b] = std::min(key, ms[b], comp);
  }

  constexpr std::pair<K, V> top() {
    if (!~ms[0]) {
      u32 idx = u32(std::ranges::find_if(ms, [](auto x) { return !!~x; }) - ms.begin());
      last = ms[idx];
      for (auto &p : vs[idx]) {
        K b = (K)std::bit_width(p.first ^ last);
        vs[b].emplace_back(p), ms[b] = std::min(p.first, ms[b], comp);
      }
      vs[idx].clear(), ms[idx] = K(-1);
    }
    return vs[0].back();
  }

  constexpr void pop() {
    top(), --s, vs[0].pop_back();
    if (vs[0].empty()) ms[0] = K(-1);
  }
};

template <class K, class V>
using rheap = std::conditional_t<std::unsigned_integral<K>, ds::radix_heap<K, V>, pqg<std::pair<K, V>>>;
template <class K, class V>
using rheapg = std::conditional_t<std::unsigned_integral<K>, ds::radix_heap<K, V, std::greater<K>>, pq<std::pair<K, V>>>;

}  // namespace tifa_libs::ds


#line 6 "src/code/edh/huffman_tree.hpp"

namespace tifa_libs {

template <class T = u64>
class huffman {
  struct YYZ {
    T weight;
    vecu ch;
    constexpr YYZ(T weight = T{}, u32 child_cnt = 0) : weight(weight), ch(child_cnt) {}
  };
  u32 cnt_w, cnt_l, ch_sz;
  vec<YYZ> data;

  template <class Res, class Op>
  constexpr vec<Res> run(Op &&operate) const {
    vec<Res> ret(cnt_w);
    std::queue<std::pair<u32, Res>> q;
    q.emplace(data.size() - 1, Res{});
    while (!q.empty()) {
      auto [now_idx, now_code] = q.front();
      auto &ch = data[now_idx].ch;
      q.pop();
      for (u32 i = 0; i < ch_sz; ++i) {
        auto &&next_child = ch[i];
        if (next_child < cnt_l) {
          if (next_child < cnt_w) ret[next_child] = operate(now_code, i);
          continue;
        } else q.emplace(next_child, operate(now_code, i));
      }
    }
    return ret;
  }

 public:
  explicit constexpr huffman(vec<T> const &weights, u32 child_sz = 2) : cnt_w((u32)weights.size()), cnt_l(), ch_sz(child_sz), data() {
    assert(1 < child_sz && child_sz < weights.size());
    for (T now : weights) data.emplace_back(now);
    for (u32 i = 0, iend = ((ch_sz - 1) - ((cnt_w - 1) % (ch_sz - 1))) % (ch_sz - 1); i < iend; ++i) data.emplace_back();
    cnt_l = (u32)data.size();
    ds::rheap<T, u32> q;
    for (u32 i = 0; i < data.size(); ++i) q.emplace(data[i].weight, i);
    while (q.size() > 1) {
      data.emplace_back(T{}, ch_sz);
      for (u32 i = 0; i < ch_sz; ++i) {
        auto [now_weight, now_idx] = q.top();
        data.back().weight += now_weight;
        data.back().ch[i] = now_idx;
        q.pop();
      }
      q.emplace(data.back().weight, data.size() - 1);
    }
  }

  constexpr vec<strn> encode(strnv char_set = "01") const {
    assert(char_set.size() == ch_sz);
    return run<strn>([&](strn const &pre_code, u32 idx) { return pre_code + char_set[idx]; });
  }
  constexpr vecu depths() const {
    return run<u32>([](u32 const &pre_depth, u32) { return pre_depth + 1; });
  }
};

}  // namespace tifa_libs


#line 4 "src/test_cpverifier/aizu/alds1_15_d.test.cpp"

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  strn s;
  std::cin >> s;
  std::map<char, u32> mp;
  for (char c : s) ++mp[c];
  vec<char> dict;
  vecu w;
  for (auto [k, v] : mp) {
    dict.push_back(k);
    w.push_back(v);
  }
  if (w.size() < 3) {
    if (w.size() == 1)
      std::cout << w[0] << "\n";
    else
      std::cout << w[0] + w[1] << "\n";
    return 0;
  }
  tifa_libs::huffman<u32> hf(w);
  auto x = hf.encode();
  u64 ans = 0;
  for (u32 i = 0; i < w.size(); ++i) ans += w[i] * x[i].size();
  std::cout << ans << '\n';
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-12 00_sample_01.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 00_sample_02.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 00_sample_03.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_00.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_01.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_02.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_03.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_04.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_05.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_06.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 01_small_07.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 02_corner_00.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 02_corner_01.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 02_corner_02.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 02_corner_03.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 02_corner_04.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 02_corner_05.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_00.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_01.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_02.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_03.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_04.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_05.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_06.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_07.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_08.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_09.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_10.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_11.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_12.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_13.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_14.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_15.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_16.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 03_random_17.in :heavy_check_mark: AC 12 ms 4 MB
g++-12 03_random_18.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 03_random_19.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 04_large_00.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 04_large_01.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 04_large_02.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 04_large_03.in :heavy_check_mark: AC 12 ms 4 MB
g++-12 04_large_04.in :heavy_check_mark: AC 12 ms 4 MB
g++-12 04_large_05.in :heavy_check_mark: AC 12 ms 4 MB
g++-12 04_large_06.in :heavy_check_mark: AC 12 ms 4 MB
g++-12 04_large_07.in :heavy_check_mark: AC 12 ms 4 MB
g++-12 04_large_08.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 04_large_09.in :heavy_check_mark: AC 11 ms 4 MB
g++-12 05_corner_01.in :heavy_check_mark: AC 10 ms 4 MB
g++-12 05_corner_02.in :heavy_check_mark: AC 9 ms 4 MB
g++-12 05_corner_03.in :heavy_check_mark: AC 9 ms 4 MB
Back to top page