shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/dp/bin-packing.test.cpp

Depends on

Code

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

// bin_packing を、品物をどの瓶に入れるかをすべて試す愚直な実装と比べる
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/dp/bin-packing.hpp"

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

int main() {
    std::mt19937_64 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 2000 && ok; rep++) {
        int n = rng() % 8;
        long long C = rng() % 15 + 1;
        std::vector<long long> w(n);
        for (auto& x : w) x = rng() % (C + 2) + 1;  // C を超える品物もたまに作る
        long long expect = -1;
        if (std::all_of(w.begin(), w.end(), [&](long long x) { return x <= C; })) {
            // 品物 i を瓶 b[i] (< n) に入れる割り当てをすべて試す
            expect = n;
            std::vector<int> b(n, 0);
            while (true) {
                std::vector<long long> load(n, 0);
                for (int i = 0; i < n; i++) load[b[i]] += w[i];
                if (std::all_of(load.begin(), load.end(), [&](long long x) { return x <= C; }))
                    expect = std::min<long long>(expect, std::count_if(load.begin(), load.end(), [](long long x) { return x > 0; }));
                int i = 0;
                while (i < n && ++b[i] == n) b[i++] = 0;
                if (i == n) break;
            }
        }
        if (bin_packing(w, C) != expect) ok = false, std::cerr << "bin_packing" << std::endl;
    }
    if (bin_packing({}, 5) != 0) ok = false;
    // 同じ瓶の数なら最後の瓶が軽い方を残さないと誤る例 (ランダムでは出にくいので決め打ちで確かめる)
    if (bin_packing({3, 4, 4, 4, 2, 9, 2}, 10) != 3) ok = false, std::cerr << "bin_packing 重さの比較" << std::endl;
    if (ok) std::cout << "Hello World" << std::endl;
}
#line 1 "test/unittest/dp/bin-packing.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// bin_packing を、品物をどの瓶に入れるかをすべて試す愚直な実装と比べる
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/dp/bin-packing.hpp"

/**
 * @file bin-packing.hpp
 * @brief 瓶詰め問題 (ビンパッキング) の最小の瓶の数 (品物が 20 個程度まで)
 */

#include <utility>
#include <vector>

/**
 * @brief 容量 C の瓶に重さ w_i の品物をすべて詰めるのに必要な瓶の最小個数 (入らない品物があれば -1) O(2^n n)
 * 品物を 1 つずつ入れる順番をすべて試し、「最後の瓶に入るなら入れ、入らなければ新しい瓶を開ける」とする
 * 最適な詰め方も瓶ごとに順に入れれば再現できるので、品物の集合ごとに (瓶の数, 最後の瓶の重さ) の辞書順最小を持てばよい
 */
inline long long bin_packing(const std::vector<long long>& w, long long C) {
    const int n = w.size();
    for (long long x : w)
        if (x > C) return -1;
    // dp[S] = 集合 S を詰めたときの (瓶の数, 最後の瓶に入っている重さ) の辞書順最小。最初は最後の瓶が満杯とみなす
    std::vector<std::pair<int, long long>> dp(1 << n, {n + 1, 0});
    dp[0] = {0, C};
    for (int S = 0; S < (1 << n); S++) {
        auto [bins, fill] = dp[S];
        if (bins > n) continue;
        for (int i = 0; i < n; i++) {
            if (S >> i & 1) continue;
            std::pair<int, long long> next = fill + w[i] <= C ? std::make_pair(bins, fill + w[i]) : std::make_pair(bins + 1, w[i]);
            if (next < dp[S | 1 << i]) dp[S | 1 << i] = next;
        }
    }
    return dp[(1 << n) - 1].first;
}
#line 7 "test/unittest/dp/bin-packing.test.cpp"

#include <algorithm>
#include <iostream>
#include <random>
#line 12 "test/unittest/dp/bin-packing.test.cpp"

int main() {
    std::mt19937_64 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 2000 && ok; rep++) {
        int n = rng() % 8;
        long long C = rng() % 15 + 1;
        std::vector<long long> w(n);
        for (auto& x : w) x = rng() % (C + 2) + 1;  // C を超える品物もたまに作る
        long long expect = -1;
        if (std::all_of(w.begin(), w.end(), [&](long long x) { return x <= C; })) {
            // 品物 i を瓶 b[i] (< n) に入れる割り当てをすべて試す
            expect = n;
            std::vector<int> b(n, 0);
            while (true) {
                std::vector<long long> load(n, 0);
                for (int i = 0; i < n; i++) load[b[i]] += w[i];
                if (std::all_of(load.begin(), load.end(), [&](long long x) { return x <= C; }))
                    expect = std::min<long long>(expect, std::count_if(load.begin(), load.end(), [](long long x) { return x > 0; }));
                int i = 0;
                while (i < n && ++b[i] == n) b[i++] = 0;
                if (i == n) break;
            }
        }
        if (bin_packing(w, C) != expect) ok = false, std::cerr << "bin_packing" << std::endl;
    }
    if (bin_packing({}, 5) != 0) ok = false;
    // 同じ瓶の数なら最後の瓶が軽い方を残さないと誤る例 (ランダムでは出にくいので決め打ちで確かめる)
    if (bin_packing({3, 4, 4, 4, 2, 9, 2}, 10) != 3) ok = false, std::cerr << "bin_packing 重さの比較" << std::endl;
    if (ok) std::cout << "Hello World" << std::endl;
}
Back to top page