shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/dp/slope-trick.test.cpp

Depends on

Code

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

// slope-trick.hpp の SlopeTrick を、整数点 [-W, W] での関数の値の表で愚直に求めたものとランダムに比べる
// 表の端の近くは平行移動で窓がはみ出して正しくなくなるが、傾きは左側で 0 以下、右側で 0 以上なので、内側の値には響かない
// あわせて、列を非減少にする最小コスト Σ|a_i - b_i| を、値の範囲での DP と比べる
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/dp/slope-trick.hpp"

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

int main() {
    std::mt19937_64 rng(314);
    bool ok = true;
    // 傾きが変わる点は、はじめ [-10, 10] にあり、平行移動 1 回で 4 以下しか動かないので [-E, E] に収まる
    const int W = 200, E = 70;  // 表の範囲と、値を比べる範囲
    for (int rep = 0; rep < 3000 && ok; rep++) {
        SlopeTrick<long long> st;
        std::vector<long long> f(2 * W + 1, 0);  // f[x + W]
        int q = rng() % 15 + 1;
        for (int i = 0; i < q; i++) {
            int type = rng() % 8;
            long long a = static_cast<long long>(rng() % 21) - 10;
            std::vector<long long> g = f;
            if (type == 0) {
                st.add_constant(a);
                for (auto& v : g) v += a;
            } else if (type == 1) {
                st.add_x_minus_a(a);
                for (int x = -W; x <= W; x++) g[x + W] += std::max(0LL, x - a);
            } else if (type == 2) {
                st.add_a_minus_x(a);
                for (int x = -W; x <= W; x++) g[x + W] += std::max(0LL, a - x);
            } else if (type == 3) {
                st.add_abs(a);
                for (int x = -W; x <= W; x++) g[x + W] += std::abs(x - a);
            } else if (type == 4) {
                st.prefix_min();
                for (int x = -W + 1; x <= W; x++) g[x + W] = std::min(g[x + W], g[x - 1 + W]);
            } else if (type == 5) {
                st.suffix_min();
                for (int x = W - 1; x >= -W; x--) g[x + W] = std::min(g[x + W], g[x + 1 + W]);
            } else {
                long long s = static_cast<long long>(rng() % 5) - 2, t = s + (type == 6 ? 0 : rng() % 3);
                if (type == 6)
                    st.shift(s);
                else
                    st.shift(s, t);
                // g(x) = min_{x - t <= y <= x - s} f(y)
                for (int x = -W; x <= W; x++) {
                    long long best = SlopeTrick<long long>::inf();
                    for (long long y = std::max<long long>(-W, x - t); y <= std::min<long long>(W, x - s); y++) best = std::min(best, f[y + W]);
                    g[x + W] = best;
                }
            }
            f = g;
            for (int x = -E; x <= E; x++) {
                if (st.get(x) != f[x + W]) ok = false;
            }
            long long mn = *std::min_element(f.begin(), f.end());
            if (st.min() != mn) ok = false;
            // 最小値をとる区間。傾きが変わる点は [-E, E] に収まるので、そこを越えて続くなら、その側は端がない
            // (表の端の近くは平行移動で窓がはみ出して正しくないので、[-E - 1, E + 1] だけを見る)
            int lo = -E - 1, hi = E + 1;
            while (lo <= E + 1 && f[lo + W] != mn) lo++;
            while (hi >= -E - 1 && f[hi + W] != mn) hi--;
            auto [l, r] = st.argmin();
            if (lo == -E - 1 ? l != -SlopeTrick<long long>::inf() : l != lo) ok = false;
            if (hi == E + 1 ? r != SlopeTrick<long long>::inf() : r != hi) ok = false;
        }
    }
    // 非減少にする最小コスト
    for (int rep = 0; rep < 1000 && ok; rep++) {
        int n = rng() % 8 + 1;
        std::vector<long long> a(n);
        for (auto& x : a) x = rng() % 10;
        SlopeTrick<long long> st;
        for (long long x : a) {
            st.prefix_min();
            st.add_abs(x);
        }
        // dp[v]: b の最後が v のときの最小コスト (b は 0 以上 9 以下にしてよい)
        std::vector<long long> dp(10, 0);
        for (long long x : a) {
            std::vector<long long> nd(10);
            long long best = SlopeTrick<long long>::inf();
            for (int v = 0; v < 10; v++) {
                best = std::min(best, dp[v]);
                nd[v] = best + std::abs(x - v);
            }
            dp = nd;
        }
        if (st.min() != *std::min_element(dp.begin(), dp.end())) ok = false;
    }
    if (ok) std::cout << "Hello World" << std::endl;
}
#line 1 "test/unittest/dp/slope-trick.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// slope-trick.hpp の SlopeTrick を、整数点 [-W, W] での関数の値の表で愚直に求めたものとランダムに比べる
// 表の端の近くは平行移動で窓がはみ出して正しくなくなるが、傾きは左側で 0 以下、右側で 0 以上なので、内側の値には響かない
// あわせて、列を非減少にする最小コスト Σ|a_i - b_i| を、値の範囲での DP と比べる
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/dp/slope-trick.hpp"

/**
 * @file slope-trick.hpp
 * @brief Slope Trick
 */

#include <algorithm>
#include <cassert>
#include <functional>
#include <limits>
#include <queue>
#include <utility>
#include <vector>

/**
 * @brief 凸な区分線形関数 f を、傾きが変わる点の多重集合として持つ (Slope Trick)
 *
 * 最小値をとる区間より左の点を L (最大ヒープ)、右の点を R (最小ヒープ) に入れ、平行移動はまとめて足す
 * はじめは f(x) = 0
 * 例:
 * SlopeTrick<long long> st;
 * for (long long a : A) {
 *     st.prefix_min();  // 非減少にするので、前の値以下ならどこでもよい
 *     st.add_abs(a);
 * }
 * long long cost = st.min();
 *
 * @tparam T 値の型 (符号付き整数)
 */
template <typename T>
class SlopeTrick {
    T min_f = 0, add_l = 0, add_r = 0;
    std::priority_queue<T> L;
    std::priority_queue<T, std::vector<T>, std::greater<T>> R;

    void push_l(T a) { L.push(a - add_l); }
    void push_r(T a) { R.push(a - add_r); }
    T top_l() const { return L.empty() ? -inf() : L.top() + add_l; }
    T top_r() const { return R.empty() ? inf() : R.top() + add_r; }
    T pop_l() {
        T a = top_l();
        L.pop();
        return a;
    }
    T pop_r() {
        T a = top_r();
        R.pop();
        return a;
    }

   public:
    /**
     * @brief 傾きが変わらない側の端を表す値 std::numeric_limits<T>::max() / 4
     */
    static constexpr T inf() { return std::numeric_limits<T>::max() / 4; }

    /**
     * @brief f の最小値
     */
    T min() const { return min_f; }
    /**
     * @brief argmin の結果。f が最小値をとる区間 [lower, upper]
     */
    struct Argmin {
        T lower;  //!< 左端 (なければ -inf())
        T upper;  //!< 右端 (なければ inf())
    };
    /**
     * @brief f が最小値をとる区間 [lower, upper]。端がなければ -inf()、inf()
     */
    Argmin argmin() const { return {top_l(), top_r()}; }
    /**
     * @brief f に定数 a を足す
     */
    void add_constant(T a) { min_f += a; }
    /**
     * @brief f に max(0, x - a) を足す O(log n)
     */
    void add_x_minus_a(T a) {
        min_f += std::max(T(0), top_l() - a);
        push_l(a);
        push_r(pop_l());
    }
    /**
     * @brief f に max(0, a - x) を足す O(log n)
     */
    void add_a_minus_x(T a) {
        min_f += std::max(T(0), a - top_r());
        push_r(a);
        push_l(pop_r());
    }
    /**
     * @brief f に |x - a| を足す O(log n)
     */
    void add_abs(T a) {
        add_x_minus_a(a);
        add_a_minus_x(a);
    }
    /**
     * @brief f(x) を min_{y <= x} f(y) にする (右側を平らにする) O(1)
     */
    void prefix_min() { R = {}; }
    /**
     * @brief f(x) を min_{y >= x} f(y) にする (左側を平らにする) O(1)
     */
    void suffix_min() { L = {}; }
    /**
     * @brief f(x) を min_{x - b <= y <= x - a} f(y) にする (スライド最小値) O(1)
     *
     * 最小値をとる区間より左を a、右を b だけ右にずらす。a <= b であること
     */
    void shift(T a, T b) {
        assert(a <= b);
        add_l += a, add_r += b;
    }
    /**
     * @brief f(x) を f(x - a) にする (a だけ右にずらす) O(1)
     */
    void shift(T a) { shift(a, a); }
    /**
     * @brief f(x) の値 O(n log n)
     */
    T get(T x) const {
        T res = min_f;
        for (auto l = L; !l.empty(); l.pop()) res += std::max(T(0), l.top() + add_l - x);
        for (auto r = R; !r.empty(); r.pop()) res += std::max(T(0), x - (r.top() + add_r));
        return res;
    }
};
#line 9 "test/unittest/dp/slope-trick.test.cpp"

#line 11 "test/unittest/dp/slope-trick.test.cpp"
#include <iostream>
#include <random>
#line 14 "test/unittest/dp/slope-trick.test.cpp"

int main() {
    std::mt19937_64 rng(314);
    bool ok = true;
    // 傾きが変わる点は、はじめ [-10, 10] にあり、平行移動 1 回で 4 以下しか動かないので [-E, E] に収まる
    const int W = 200, E = 70;  // 表の範囲と、値を比べる範囲
    for (int rep = 0; rep < 3000 && ok; rep++) {
        SlopeTrick<long long> st;
        std::vector<long long> f(2 * W + 1, 0);  // f[x + W]
        int q = rng() % 15 + 1;
        for (int i = 0; i < q; i++) {
            int type = rng() % 8;
            long long a = static_cast<long long>(rng() % 21) - 10;
            std::vector<long long> g = f;
            if (type == 0) {
                st.add_constant(a);
                for (auto& v : g) v += a;
            } else if (type == 1) {
                st.add_x_minus_a(a);
                for (int x = -W; x <= W; x++) g[x + W] += std::max(0LL, x - a);
            } else if (type == 2) {
                st.add_a_minus_x(a);
                for (int x = -W; x <= W; x++) g[x + W] += std::max(0LL, a - x);
            } else if (type == 3) {
                st.add_abs(a);
                for (int x = -W; x <= W; x++) g[x + W] += std::abs(x - a);
            } else if (type == 4) {
                st.prefix_min();
                for (int x = -W + 1; x <= W; x++) g[x + W] = std::min(g[x + W], g[x - 1 + W]);
            } else if (type == 5) {
                st.suffix_min();
                for (int x = W - 1; x >= -W; x--) g[x + W] = std::min(g[x + W], g[x + 1 + W]);
            } else {
                long long s = static_cast<long long>(rng() % 5) - 2, t = s + (type == 6 ? 0 : rng() % 3);
                if (type == 6)
                    st.shift(s);
                else
                    st.shift(s, t);
                // g(x) = min_{x - t <= y <= x - s} f(y)
                for (int x = -W; x <= W; x++) {
                    long long best = SlopeTrick<long long>::inf();
                    for (long long y = std::max<long long>(-W, x - t); y <= std::min<long long>(W, x - s); y++) best = std::min(best, f[y + W]);
                    g[x + W] = best;
                }
            }
            f = g;
            for (int x = -E; x <= E; x++) {
                if (st.get(x) != f[x + W]) ok = false;
            }
            long long mn = *std::min_element(f.begin(), f.end());
            if (st.min() != mn) ok = false;
            // 最小値をとる区間。傾きが変わる点は [-E, E] に収まるので、そこを越えて続くなら、その側は端がない
            // (表の端の近くは平行移動で窓がはみ出して正しくないので、[-E - 1, E + 1] だけを見る)
            int lo = -E - 1, hi = E + 1;
            while (lo <= E + 1 && f[lo + W] != mn) lo++;
            while (hi >= -E - 1 && f[hi + W] != mn) hi--;
            auto [l, r] = st.argmin();
            if (lo == -E - 1 ? l != -SlopeTrick<long long>::inf() : l != lo) ok = false;
            if (hi == E + 1 ? r != SlopeTrick<long long>::inf() : r != hi) ok = false;
        }
    }
    // 非減少にする最小コスト
    for (int rep = 0; rep < 1000 && ok; rep++) {
        int n = rng() % 8 + 1;
        std::vector<long long> a(n);
        for (auto& x : a) x = rng() % 10;
        SlopeTrick<long long> st;
        for (long long x : a) {
            st.prefix_min();
            st.add_abs(x);
        }
        // dp[v]: b の最後が v のときの最小コスト (b は 0 以上 9 以下にしてよい)
        std::vector<long long> dp(10, 0);
        for (long long x : a) {
            std::vector<long long> nd(10);
            long long best = SlopeTrick<long long>::inf();
            for (int v = 0; v < 10; v++) {
                best = std::min(best, dp[v]);
                nd[v] = best + std::abs(x - v);
            }
            dp = nd;
        }
        if (st.min() != *std::min_element(dp.begin(), dp.end())) ok = false;
    }
    if (ok) std::cout << "Hello World" << std::endl;
}
Back to top page