shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/dp/maximum-subarray.test.cpp

Depends on

Code

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

// maximum_subarray を、すべての区間を調べる愚直な方法とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/dp/maximum-subarray.hpp"

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

bool ok = true;

void check(bool cond, const char* what) {
    if (!cond) {
        if (ok) std::cerr << "failed: " << what << std::endl;
        ok = false;
    }
}

// constexpr で使えること
static_assert(maximum_subarray(std::array<int, 5>{-1, 3, -2, 4, -5}) == 5);
static_assert(maximum_subarray(std::array<int, 3>{-5, -2, -9}, 0) == 0);

int main() {
    std::mt19937_64 rng(314);
    for (int rep = 0; rep < 1000; rep++) {
        int n = rng() % 10 + 1;
        std::vector<long long> v(n);
        for (auto& x : v) x = static_cast<long long>(rng() % 21) - 10;

        // 空でない版
        long long best_nonempty = v[0];
        for (int l = 0; l < n; l++) {
            long long s = 0;
            for (int r = l; r < n; r++) {
                s += v[r];
                best_nonempty = std::max(best_nonempty, s);
            }
        }
        check(maximum_subarray(v) == best_nonempty, "maximum_subarray(c)");

        // 空の部分列を許す版: 全区間の和の最大値と init の大きい方
        long long init = static_cast<long long>(rng() % 7) - 3;
        long long best = init;
        for (int l = 0; l < n; l++) {
            long long s = init;
            for (int r = l; r < n; r++) {
                s += v[r];
                best = std::max(best, s);
            }
        }
        check(maximum_subarray(v, init) == best, "maximum_subarray(c, init)");
    }

    // 境界: 空、すべて負
    check(maximum_subarray(std::vector<int>{}, 0) == 0, "maximum_subarray 空");
    check(maximum_subarray(std::vector<int>{-5, -2, -9}) == -2, "maximum_subarray すべて負");
    check(maximum_subarray(std::vector<int>{-5, -2, -9}, 0) == 0, "maximum_subarray すべて負、空を許す");
    check(maximum_subarray(std::vector<int>{-2, 1, -3, 4, -1, 2, 1, -5, 4}) == 6, "maximum_subarray ドキュメントの例");

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

// maximum_subarray を、すべての区間を調べる愚直な方法とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/dp/maximum-subarray.hpp"

/**
 * @file maximum-subarray.hpp
 * @brief 部分配列の和の最大値 (Kadane のアルゴリズム)
 */

#include <cassert>

/**
 * @brief 空でない部分配列の和の最大値 O(n)
 * @param v begin()、end() を持つ空でないコンテナ
 */
template <class C>
constexpr typename C::value_type maximum_subarray(const C& v) {
    assert(!v.empty());
    auto itr = v.begin();
    // tmp は、いま見ている要素で終わる部分配列の和の最大値
    typename C::value_type tmp = *itr++;
    typename C::value_type res = tmp;
    while (itr != v.end()) {
        tmp += *itr;
        if (tmp < *itr) tmp = *itr;
        if (res < tmp) res = tmp;
        ++itr;
    }
    return res;
}

/**
 * @brief 空の部分配列 (和 init) も許した部分配列の和の最大値 O(n)
 * @param v begin()、end() を持つコンテナ
 * @param init 空の部分配列の和
 */
template <class C>
constexpr typename C::value_type maximum_subarray(const C& v, typename C::value_type init) {
    typename C::value_type res = init, tmp = init;
    for (const auto& a : v) {
        tmp += a;
        if (tmp < init) tmp = init;
        if (res < tmp) res = tmp;
    }
    return res;
}
#line 7 "test/unittest/dp/maximum-subarray.test.cpp"

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

bool ok = true;

void check(bool cond, const char* what) {
    if (!cond) {
        if (ok) std::cerr << "failed: " << what << std::endl;
        ok = false;
    }
}

// constexpr で使えること
static_assert(maximum_subarray(std::array<int, 5>{-1, 3, -2, 4, -5}) == 5);
static_assert(maximum_subarray(std::array<int, 3>{-5, -2, -9}, 0) == 0);

int main() {
    std::mt19937_64 rng(314);
    for (int rep = 0; rep < 1000; rep++) {
        int n = rng() % 10 + 1;
        std::vector<long long> v(n);
        for (auto& x : v) x = static_cast<long long>(rng() % 21) - 10;

        // 空でない版
        long long best_nonempty = v[0];
        for (int l = 0; l < n; l++) {
            long long s = 0;
            for (int r = l; r < n; r++) {
                s += v[r];
                best_nonempty = std::max(best_nonempty, s);
            }
        }
        check(maximum_subarray(v) == best_nonempty, "maximum_subarray(c)");

        // 空の部分列を許す版: 全区間の和の最大値と init の大きい方
        long long init = static_cast<long long>(rng() % 7) - 3;
        long long best = init;
        for (int l = 0; l < n; l++) {
            long long s = init;
            for (int r = l; r < n; r++) {
                s += v[r];
                best = std::max(best, s);
            }
        }
        check(maximum_subarray(v, init) == best, "maximum_subarray(c, init)");
    }

    // 境界: 空、すべて負
    check(maximum_subarray(std::vector<int>{}, 0) == 0, "maximum_subarray 空");
    check(maximum_subarray(std::vector<int>{-5, -2, -9}) == -2, "maximum_subarray すべて負");
    check(maximum_subarray(std::vector<int>{-5, -2, -9}, 0) == 0, "maximum_subarray すべて負、空を許す");
    check(maximum_subarray(std::vector<int>{-2, 1, -3, 4, -1, 2, 1, -5, 4}) == 6, "maximum_subarray ドキュメントの例");

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