競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// MexUpdate を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/data_structures/mex-update.hpp"
#include <iostream>
#include <random>
#include <set>
#include <vector>
// v に含まれない最小の非負整数
template <typename T>
T naive_mex(const std::vector<T>& v) {
std::set<T> s(v.begin(), v.end());
T x = 0;
while (s.count(x)) x++;
return x;
}
template <typename T>
bool run(std::mt19937& rng) {
for (int rep = 0; rep < 2000; rep++) {
int n = rng() % 8 + 1;
// 範囲外の値 (負の値や n より大きい値) も混ぜる
auto random_value = [&]() -> T { return static_cast<T>(static_cast<int>(rng() % (n + 6)) - 3); };
std::vector<T> v;
MexUpdate<T> mu;
if (rep % 2 == 0) {
// MexUpdate(n): 値はどれも mex に影響しない値で初期化される
mu = MexUpdate<T>(n);
v.assign(n, static_cast<T>(-1));
if (mu.mex() != 0) return false;
} else {
v.resize(n);
for (auto& x : v) x = random_value();
mu = MexUpdate<T>(v);
}
if (mu.mex() != naive_mex(v)) return false;
for (int t = 0; t < 20; t++) {
int k = rng() % n;
T x = random_value();
mu.set(k, x);
v[k] = x;
if (mu.mex() != naive_mex(v)) return false;
}
}
return true;
}
int main() {
std::mt19937 rng(314);
// 符号なしの型では、負の値は大きな値になる (範囲外の値として扱われる)
bool ok = run<int>(rng) && run<long long>(rng) && run<unsigned int>(rng) && run<unsigned long long>(rng);
// 0..n-1 がすべて揃うと mex は n
MexUpdate<int> full(std::vector<int>{2, 0, 1});
ok = ok && full.mex() == 3;
full.set(1, 3);
ok = ok && full.mex() == 0;
// 長さ 0 と既定構築は mex が 0
ok = ok && MexUpdate<int>().mex() == 0 && MexUpdate<int>(0).mex() == 0;
ok = ok && MexUpdate<int>(std::vector<int>{}).mex() == 0 && MexUpdate<unsigned int>().mex() == 0u;
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}#line 1 "test/unittest/data_structures/mex-update.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// MexUpdate を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/data_structures/mex-update.hpp"
/**
* @file mex-update.hpp
* @brief 1点更新・全体の mex 取得
*/
#include <cassert>
#include <limits>
#include <map>
#include <set>
#include <type_traits>
#include <vector>
template <typename T>
class MexUpdate {
static_assert(std::is_integral_v<T>, "MexUpdate: T must be an integer type");
public:
using value_type = T;
private:
inline constexpr static value_type e = std::numeric_limits<value_type>::max();
long long n;
std::vector<value_type> v;
std::set<int> s;
std::map<int, int> m;
// x が 0 以上 n 以下か (T が符号なしでも警告が出ないように比べる)
bool in_range(const value_type& x) const {
if constexpr (std::is_signed_v<value_type>) {
if (x < 0) return false;
}
return static_cast<unsigned long long>(x) <= static_cast<unsigned long long>(n);
}
public:
/**
* @brief 長さ 0 (mex は 0)
*/
MexUpdate() : MexUpdate(0) {}
/**
* @brief 長さ n で、どの要素もまだ値を持たない (mex は 0)。n = 0 でもよい
*/
explicit MexUpdate(long long n) : n(n), v(n, e) {
assert(n >= 0);
assert(static_cast<unsigned long long>(n) + 1 < static_cast<unsigned long long>(e));
std::set<int>::iterator hint = s.end();
for (long long i = n; i >= 0; --i) {
hint = s.emplace_hint(hint, static_cast<int>(i));
}
}
explicit MexUpdate(const std::vector<value_type>& init) : n(init.size()), v(init) {
assert(static_cast<unsigned long long>(n) + 1 < static_cast<unsigned long long>(e));
std::set<int>::iterator hint = s.end();
for (long long i = n; i >= 0; --i) {
hint = s.emplace_hint(hint, static_cast<int>(i));
}
for (const auto& i : init) {
if (in_range(i)) {
m[i]++;
s.erase(i);
}
}
}
void set(long long k, const value_type& x) {
assert(0 <= k && k < n);
if (in_range(v[k])) {
int v_k = v[k];
auto itr = m.find(v_k);
if (itr->second > 1) {
itr->second--;
} else {
m.erase(itr);
s.insert(v_k);
}
}
if (in_range(x)) {
int xx = x;
auto itr = m.find(xx);
if (itr == m.end()) {
m[xx]++;
s.erase(xx);
} else {
itr->second++;
}
}
v[k] = x;
}
value_type mex() const {
return *s.begin();
}
};
#line 7 "test/unittest/data_structures/mex-update.test.cpp"
#include <iostream>
#include <random>
#line 12 "test/unittest/data_structures/mex-update.test.cpp"
// v に含まれない最小の非負整数
template <typename T>
T naive_mex(const std::vector<T>& v) {
std::set<T> s(v.begin(), v.end());
T x = 0;
while (s.count(x)) x++;
return x;
}
template <typename T>
bool run(std::mt19937& rng) {
for (int rep = 0; rep < 2000; rep++) {
int n = rng() % 8 + 1;
// 範囲外の値 (負の値や n より大きい値) も混ぜる
auto random_value = [&]() -> T { return static_cast<T>(static_cast<int>(rng() % (n + 6)) - 3); };
std::vector<T> v;
MexUpdate<T> mu;
if (rep % 2 == 0) {
// MexUpdate(n): 値はどれも mex に影響しない値で初期化される
mu = MexUpdate<T>(n);
v.assign(n, static_cast<T>(-1));
if (mu.mex() != 0) return false;
} else {
v.resize(n);
for (auto& x : v) x = random_value();
mu = MexUpdate<T>(v);
}
if (mu.mex() != naive_mex(v)) return false;
for (int t = 0; t < 20; t++) {
int k = rng() % n;
T x = random_value();
mu.set(k, x);
v[k] = x;
if (mu.mex() != naive_mex(v)) return false;
}
}
return true;
}
int main() {
std::mt19937 rng(314);
// 符号なしの型では、負の値は大きな値になる (範囲外の値として扱われる)
bool ok = run<int>(rng) && run<long long>(rng) && run<unsigned int>(rng) && run<unsigned long long>(rng);
// 0..n-1 がすべて揃うと mex は n
MexUpdate<int> full(std::vector<int>{2, 0, 1});
ok = ok && full.mex() == 3;
full.set(1, 3);
ok = ok && full.mex() == 0;
// 長さ 0 と既定構築は mex が 0
ok = ok && MexUpdate<int>().mex() == 0 && MexUpdate<int>(0).mex() == 0;
ok = ok && MexUpdate<int>(std::vector<int>{}).mex() == 0 && MexUpdate<unsigned int>().mex() == 0u;
if (ok)
std::cout << "Hello World" << std::endl;
else
std::cerr << "failed" << std::endl;
}