shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/utility/compress.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// Compress を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/utility/compress.hpp"

#include <algorithm>
#include <iostream>
#include <random>
#include <string>
#include <vector>

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 1000 && ok; rep++) {
        int n = rng() % 20;
        int range = rng() % 30 + 1;
        std::vector<int> a(n);
        for (auto& v : a) v = static_cast<int>(rng() % range) - range / 2;

        // コンストラクタで作る場合と、add を混ぜて作る場合
        Compress<int> c1(a);
        Compress<int> c2;
        int half = n / 2;
        for (int i = 0; i < half; i++) c2.add(a[i]);
        c2.add(std::vector<int>(a.begin() + half, a.end()));
        c2.build();

        std::vector<int> xs = a;
        std::sort(xs.begin(), xs.end());
        xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
        int m = xs.size();

        for (const Compress<int>* c : {&c1, &c2}) {
            ok = ok && c->size() == m && c->values() == xs;
            for (int i = 0; i < m; i++) ok = ok && (*c)[i] == xs[i] && c->id(xs[i]) == i;
            for (int x = -range; x <= range; x++) {
                int lo = 0, up = 0;
                while (lo < m && xs[lo] < x) lo++;
                while (up < m && xs[up] <= x) up++;
                bool has = std::count(a.begin(), a.end(), x) > 0;
                ok = ok && c->bisect_left(x) == lo && c->bisect_right(x) == up && c->contains(x) == has;
            }
            std::vector<long long> ids = c->ids(a);
            for (int i = 0; i < n; i++) ok = ok && xs[ids[i]] == a[i];
        }
    }

    // 例: 文字列も圧縮できる
    Compress<std::string> cs(std::vector<std::string>{"b", "a", "c", "a"});
    ok = ok && cs.size() == 3 && cs.id("c") == 2 && cs[0] == "a" && !cs.contains("d") && cs.bisect_left("bb") == 2;

    if (ok)
        std::cout << "Hello World" << std::endl;
    else
        std::cerr << "failed" << std::endl;
}
#line 1 "test/unittest/utility/compress.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// Compress を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/utility/compress.hpp"

/**
 * @file compress.hpp
 * @brief 座標圧縮
 */

#include <algorithm>
#include <cassert>
#include <vector>

/**
 * @brief 座標圧縮
 *
 * 値を add で登録し、build でソートと重複除去をしてから使う
 * 例:
 * Compress<long long> cp(A);  // A を登録して build まで行う
 * long long i = cp.id(A[0]);  // 0 <= i < cp.size()
 *
 * @tparam T 値の型 (< で比較できること)
 */
template <typename T>
class Compress {
    std::vector<T> xs;
    bool built = false;

   public:
    Compress() = default;
    /**
     * @brief v の値を登録して build する
     */
    explicit Compress(const std::vector<T>& v) : xs(v) { build(); }

    /**
     * @brief 値を 1 つ登録する。build の前に呼ぶ
     */
    void add(const T& x) {
        assert(!built);
        xs.push_back(x);
    }
    /**
     * @brief v の値をすべて登録する。build の前に呼ぶ
     */
    void add(const std::vector<T>& v) {
        assert(!built);
        xs.insert(xs.end(), v.begin(), v.end());
    }
    /**
     * @brief 登録した値をソートして重複を除く O(n log n)
     */
    void build() {
        assert(!built);
        std::sort(xs.begin(), xs.end());
        xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
        built = true;
    }

    /**
     * @brief 異なる値の個数
     */
    long long size() const {
        assert(built);
        return xs.size();
    }
    /**
     * @brief 登録した値 x の番号 O(log n)。登録していない値なら assert で落ちる
     */
    long long id(const T& x) const {
        const long long i = bisect_left(x);
        assert(i < size() && !(x < xs[i]));
        return i;
    }
    /**
     * @brief x 以上で最小の値の番号 O(log n)。なければ size()
     */
    long long bisect_left(const T& x) const {
        assert(built);
        return std::lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    }
    /**
     * @brief x より大きい最小の値の番号 O(log n)。なければ size()
     */
    long long bisect_right(const T& x) const {
        assert(built);
        return std::upper_bound(xs.begin(), xs.end(), x) - xs.begin();
    }
    /**
     * @brief x を登録したか O(log n)
     */
    bool contains(const T& x) const {
        const long long i = bisect_left(x);
        return i < size() && !(x < xs[i]);
    }
    /**
     * @brief 番号 i の値
     */
    const T& operator[](long long i) const {
        assert(built && 0 <= i && i < size());
        return xs[i];
    }
    /**
     * @brief v の各値を番号に置き換えた列
     */
    std::vector<long long> ids(const std::vector<T>& v) const {
        std::vector<long long> res(v.size());
        for (long long i = 0; i < static_cast<long long>(v.size()); i++) res[i] = id(v[i]);
        return res;
    }
    /**
     * @brief ソート済みで重複のない値の列
     */
    const std::vector<T>& values() const {
        assert(built);
        return xs;
    }
};
#line 7 "test/unittest/utility/compress.test.cpp"

#line 9 "test/unittest/utility/compress.test.cpp"
#include <iostream>
#include <random>
#include <string>
#line 13 "test/unittest/utility/compress.test.cpp"

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 1000 && ok; rep++) {
        int n = rng() % 20;
        int range = rng() % 30 + 1;
        std::vector<int> a(n);
        for (auto& v : a) v = static_cast<int>(rng() % range) - range / 2;

        // コンストラクタで作る場合と、add を混ぜて作る場合
        Compress<int> c1(a);
        Compress<int> c2;
        int half = n / 2;
        for (int i = 0; i < half; i++) c2.add(a[i]);
        c2.add(std::vector<int>(a.begin() + half, a.end()));
        c2.build();

        std::vector<int> xs = a;
        std::sort(xs.begin(), xs.end());
        xs.erase(std::unique(xs.begin(), xs.end()), xs.end());
        int m = xs.size();

        for (const Compress<int>* c : {&c1, &c2}) {
            ok = ok && c->size() == m && c->values() == xs;
            for (int i = 0; i < m; i++) ok = ok && (*c)[i] == xs[i] && c->id(xs[i]) == i;
            for (int x = -range; x <= range; x++) {
                int lo = 0, up = 0;
                while (lo < m && xs[lo] < x) lo++;
                while (up < m && xs[up] <= x) up++;
                bool has = std::count(a.begin(), a.end(), x) > 0;
                ok = ok && c->bisect_left(x) == lo && c->bisect_right(x) == up && c->contains(x) == has;
            }
            std::vector<long long> ids = c->ids(a);
            for (int i = 0; i < n; i++) ok = ok && xs[ids[i]] == a[i];
        }
    }

    // 例: 文字列も圧縮できる
    Compress<std::string> cs(std::vector<std::string>{"b", "a", "c", "a"});
    ok = ok && cs.size() == 3 && cs.id("c") == 2 && cs[0] == "a" && !cs.contains("d") && cs.bisect_left("bb") == 2;

    if (ok)
        std::cout << "Hello World" << std::endl;
    else
        std::cerr << "failed" << std::endl;
}
Back to top page