競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// PriorityCounter (MaxCounter / MinCounter) を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/data_structures/priority_counter.hpp"
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <random>
#include <vector>
// 全体の個数から、Compare で大きい方から K 個の値とその個数を求める
template <size_t K, class Compare>
std::vector<std::pair<int, long long>> top_k(const std::map<int, long long>& m) {
std::vector<std::pair<int, long long>> v(m.begin(), m.end());
std::sort(v.begin(), v.end(), [](const auto& a, const auto& b) { return Compare()(b.first, a.first); });
if (v.size() > K) v.resize(K);
return v;
}
template <size_t K, class Compare>
bool same(const PriorityCounter<int, K, Compare>& pc, const std::map<int, long long>& m) {
auto expect = top_k<K, Compare>(m);
if (pc.size() != static_cast<int>(expect.size())) return false;
long long total = 0;
for (size_t i = 0; i < expect.size(); i++) {
if (pc[i] != expect[i]) return false;
total += expect[i].second;
}
if (pc.total() != total) return false;
// 使われていない場所の個数は 0
for (size_t i = expect.size(); i < K; i++) {
if (pc[i].second != 0) return false;
}
return true;
}
template <size_t K, class Compare>
bool run(std::mt19937& rng) {
using PC = PriorityCounter<int, K, Compare>;
{
PC empty;
if (!same<K, Compare>(empty, {})) return false;
PC one(7);
if (!same<K, Compare>(one, {{7, 1}})) return false;
}
for (int rep = 0; rep < 2000; rep++) {
PC a, b;
std::map<int, long long> ma, mb;
int q = rng() % 12;
for (int t = 0; t < q; t++) {
int x = rng() % 8;
long long c = rng() % 3 + 1;
if (rng() % 2) {
a.inc(x, c);
ma[x] += c;
} else {
// 個数を省略すると 1
a.inc(x);
ma[x] += 1;
}
if (!same<K, Compare>(a, ma)) return false;
int y = rng() % 8;
b.inc(y, 2);
mb[y] += 2;
}
std::map<int, long long> sum = ma;
for (auto [x, c] : mb) sum[x] += c;
if (!same<K, Compare>(a + b, sum)) return false;
a += b;
if (!same<K, Compare>(a, sum)) return false;
}
return true;
}
// constexpr で使えること (C++17 の定数式で inc・+・size を評価する)
constexpr bool constexpr_check() {
MaxCounter<int, 2> a;
a.inc(5);
a.inc(1);
a.inc(9); // 上位 2 個 {9, 5} が残り、1 は押し出される
a.inc(5, 2);
MaxCounter<int, 2> b(7);
MaxCounter<int, 2> c = a + b; // {9, 7}
return a.size() == 2 && a[0] == std::pair<int, long long>(9, 1) && a[1] == std::pair<int, long long>(5, 3) &&
c[0] == std::pair<int, long long>(9, 1) && c[1] == std::pair<int, long long>(7, 1) &&
a.total() == 4 && c.total() == 2 && MinCounter<int, 3>().size() == 0 && MinCounter<int, 3>().total() == 0;
}
static_assert(constexpr_check());
int main() {
std::mt19937 rng(314);
bool ok = true;
ok = ok && run<1, std::less<int>>(rng);
ok = ok && run<2, std::less<int>>(rng);
ok = ok && run<3, std::less<int>>(rng);
ok = ok && run<5, std::less<int>>(rng);
ok = ok && run<1, std::greater<int>>(rng);
ok = ok && run<3, std::greater<int>>(rng);
// 別名: MaxCounter は大きい方、MinCounter は小さい方を残す
MaxCounter<int, 2> mx;
MinCounter<int, 2> mn;
for (int x : {5, 1, 9, 5, 3}) {
mx.inc(x);
mn.inc(x);
}
ok = ok && mx.size() == 2 && mx[0] == std::make_pair(9, 1LL) && mx[1] == std::make_pair(5, 2LL);
ok = ok && mn.size() == 2 && mn[0] == std::make_pair(1, 1LL) && mn[1] == std::make_pair(3, 1LL);
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}#line 1 "test/unittest/data_structures/priority_counter.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// PriorityCounter (MaxCounter / MinCounter) を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/data_structures/priority_counter.hpp"
/**
* @file priority_counter.hpp
* @brief 上位 K 個の値とその個数
*/
#include <array>
#include <cassert>
#include <cstddef>
#include <functional>
#include <type_traits>
#include <utility>
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#line 10 "src/utility/traits.hpp"
#include <iterator>
#include <set>
#line 14 "src/utility/traits.hpp"
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 16 "src/data_structures/priority_counter.hpp"
template <typename T, std::size_t K = 1, class Compare = std::less<T>, ENABLE_IF_T(K >= 1)>
struct PriorityCounter {
using key_type = T;
using mapped_type = long long;
using value_type = std::pair<key_type, mapped_type>;
inline constexpr static std::size_t k = K;
private:
// C++17 の constexpr コンストラクタではすべてのメンバの初期化が必要なので、値初期化しておく
std::array<value_type, K> data{};
public:
/** @brief 上位から i 番目の値と個数。size() <= i < K では個数は 0 */
constexpr const value_type& operator[](long long i) const {
assert(0 <= i && i < static_cast<long long>(K));
return data[i];
}
constexpr PriorityCounter() {}
explicit constexpr PriorityCounter(const key_type& x) : PriorityCounter() {
data[0].first = x;
data[0].second = 1;
}
constexpr long long size() const {
for (std::size_t i = 0; i < k; i++) {
if (data[i].second == 0) {
return static_cast<long long>(i);
}
}
return k;
}
/** @brief 保持している上位 K 種類の個数の総和 O(K) */
constexpr mapped_type total() const {
mapped_type result = 0;
for (const auto& entry : data) result += entry.second;
return result;
}
constexpr void inc(const key_type& x, const mapped_type& c = 1) {
assert(c > 0);
bool f = false;
key_type pre_key{};
mapped_type pre_mapped{};
for (std::size_t i = 0; i < k; i++) {
if (f) {
// std::swap は C++17 では constexpr でないので手で入れ替える
key_type tmp_key = data[i].first;
data[i].first = pre_key;
pre_key = tmp_key;
mapped_type tmp_mapped = data[i].second;
data[i].second = pre_mapped;
pre_mapped = tmp_mapped;
continue;
}
if (data[i].second == 0) {
data[i].first = x;
data[i].second += c;
return;
}
if (Compare()(data[i].first, x)) {
pre_key = data[i].first;
pre_mapped = data[i].second;
data[i].first = x;
data[i].second = c;
f = true;
} else if (!Compare()(x, data[i].first)) {
data[i].second += c;
return;
}
}
}
constexpr PriorityCounter& operator+=(const PriorityCounter& o) {
for (std::size_t i = 0; i < k; i++) {
if (o.data[i].second == 0) break;
this->inc(o.data[i].first, o.data[i].second);
}
return *this;
}
constexpr friend PriorityCounter operator+(const PriorityCounter& a, const PriorityCounter& b) {
PriorityCounter res(a);
return res += b;
}
};
template <typename T, std::size_t K = 1>
using MaxCounter = PriorityCounter<T, K>;
template <typename T, std::size_t K = 1>
using MinCounter = PriorityCounter<T, K, std::greater<T>>;
#line 7 "test/unittest/data_structures/priority_counter.test.cpp"
#include <algorithm>
#line 10 "test/unittest/data_structures/priority_counter.test.cpp"
#include <iostream>
#include <map>
#include <random>
#include <vector>
// 全体の個数から、Compare で大きい方から K 個の値とその個数を求める
template <size_t K, class Compare>
std::vector<std::pair<int, long long>> top_k(const std::map<int, long long>& m) {
std::vector<std::pair<int, long long>> v(m.begin(), m.end());
std::sort(v.begin(), v.end(), [](const auto& a, const auto& b) { return Compare()(b.first, a.first); });
if (v.size() > K) v.resize(K);
return v;
}
template <size_t K, class Compare>
bool same(const PriorityCounter<int, K, Compare>& pc, const std::map<int, long long>& m) {
auto expect = top_k<K, Compare>(m);
if (pc.size() != static_cast<int>(expect.size())) return false;
long long total = 0;
for (size_t i = 0; i < expect.size(); i++) {
if (pc[i] != expect[i]) return false;
total += expect[i].second;
}
if (pc.total() != total) return false;
// 使われていない場所の個数は 0
for (size_t i = expect.size(); i < K; i++) {
if (pc[i].second != 0) return false;
}
return true;
}
template <size_t K, class Compare>
bool run(std::mt19937& rng) {
using PC = PriorityCounter<int, K, Compare>;
{
PC empty;
if (!same<K, Compare>(empty, {})) return false;
PC one(7);
if (!same<K, Compare>(one, {{7, 1}})) return false;
}
for (int rep = 0; rep < 2000; rep++) {
PC a, b;
std::map<int, long long> ma, mb;
int q = rng() % 12;
for (int t = 0; t < q; t++) {
int x = rng() % 8;
long long c = rng() % 3 + 1;
if (rng() % 2) {
a.inc(x, c);
ma[x] += c;
} else {
// 個数を省略すると 1
a.inc(x);
ma[x] += 1;
}
if (!same<K, Compare>(a, ma)) return false;
int y = rng() % 8;
b.inc(y, 2);
mb[y] += 2;
}
std::map<int, long long> sum = ma;
for (auto [x, c] : mb) sum[x] += c;
if (!same<K, Compare>(a + b, sum)) return false;
a += b;
if (!same<K, Compare>(a, sum)) return false;
}
return true;
}
// constexpr で使えること (C++17 の定数式で inc・+・size を評価する)
constexpr bool constexpr_check() {
MaxCounter<int, 2> a;
a.inc(5);
a.inc(1);
a.inc(9); // 上位 2 個 {9, 5} が残り、1 は押し出される
a.inc(5, 2);
MaxCounter<int, 2> b(7);
MaxCounter<int, 2> c = a + b; // {9, 7}
return a.size() == 2 && a[0] == std::pair<int, long long>(9, 1) && a[1] == std::pair<int, long long>(5, 3) &&
c[0] == std::pair<int, long long>(9, 1) && c[1] == std::pair<int, long long>(7, 1) &&
a.total() == 4 && c.total() == 2 && MinCounter<int, 3>().size() == 0 && MinCounter<int, 3>().total() == 0;
}
static_assert(constexpr_check());
int main() {
std::mt19937 rng(314);
bool ok = true;
ok = ok && run<1, std::less<int>>(rng);
ok = ok && run<2, std::less<int>>(rng);
ok = ok && run<3, std::less<int>>(rng);
ok = ok && run<5, std::less<int>>(rng);
ok = ok && run<1, std::greater<int>>(rng);
ok = ok && run<3, std::greater<int>>(rng);
// 別名: MaxCounter は大きい方、MinCounter は小さい方を残す
MaxCounter<int, 2> mx;
MinCounter<int, 2> mn;
for (int x : {5, 1, 9, 5, 3}) {
mx.inc(x);
mn.inc(x);
}
ok = ok && mx.size() == 2 && mx[0] == std::make_pair(9, 1LL) && mx[1] == std::make_pair(5, 2LL);
ok = ok && mn.size() == 2 && mn[0] == std::make_pair(1, 1LL) && mn[1] == std::make_pair(3, 1LL);
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}