shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/dp/longest-increasing-subsequence.test.cpp

Depends on

Code

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

// longest_increasing_subsequence とその復元、長さの列を、すべての部分列を調べる愚直な方法とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/dp/longest-increasing-subsequence.hpp"

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

template <typename Compare>
bool check(const std::vector<int>& a, bool strict, Compare comp) {
    int n = a.size();
    auto ok_pair = [&](long long i, long long j) { return strict ? comp(a[i], a[j]) : !comp(a[j], a[i]); };
    // 愚直に a[i] で終わる最長の長さを求める
    std::vector<long long> len(n);
    for (int j = 0; j < n; j++) {
        len[j] = 1;
        for (int i = 0; i < j; i++) {
            if (ok_pair(i, j)) len[j] = std::max(len[j], len[i] + 1);
        }
    }
    long long best = n == 0 ? 0 : *std::max_element(len.begin(), len.end());
    if (longest_increasing_subsequence_lengths(a, strict, comp) != len) return false;
    if (longest_increasing_subsequence(a, strict, comp) != best) return false;
    auto [length, idx] = longest_increasing_subsequence_restore(a, strict, comp);
    if (length != best || static_cast<long long>(idx.size()) != best) return false;
    for (long long k = 0; k < static_cast<long long>(idx.size()); k++) {
        if (idx[k] < 0 || idx[k] >= n) return false;
        if (k > 0 && (idx[k - 1] >= idx[k] || !ok_pair(idx[k - 1], idx[k]))) return false;
    }
    return true;
}

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 3000 && ok; rep++) {
        int n = rng() % 15;
        int range = rng() % 10 + 1;
        std::vector<int> a(n);
        for (auto& v : a) v = rng() % range;
        for (bool strict : {true, false}) {
            ok = ok && check(a, strict, std::less<int>());
            ok = ok && check(a, strict, std::greater<int>());
        }
    }

    // 例
    std::vector<int> a = {3, 1, 4, 1, 5, 9, 2, 6};
    ok = ok && longest_increasing_subsequence(a) == 4;
    ok = ok && longest_increasing_subsequence(a, false) == 4;
    ok = ok && longest_increasing_subsequence(std::vector<int>{2, 2, 2}, false) == 3;
    ok = ok && longest_increasing_subsequence(std::vector<int>{2, 2, 2}) == 1;
    ok = ok && longest_increasing_subsequence(a, true, std::greater<int>()) == 2;

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

// longest_increasing_subsequence とその復元、長さの列を、すべての部分列を調べる愚直な方法とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/dp/longest-increasing-subsequence.hpp"

/**
 * @file longest-increasing-subsequence.hpp
 * @brief 最長増加部分列 (LIS)
 */

#include <algorithm>
#include <functional>
#include <vector>

/**
 * @brief a[i] で終わる最長増加部分列の長さを、各 i について求める O(n log n)
 * @param strict true なら狭義単調増加 (comp(前, 後) が成り立つ)、false なら広義単調増加 (comp(後, 前) が成り立たない)
 * @param comp 大小の比較。std::greater<T>() を渡すと減少列になる
 */
template <typename T, typename Compare = std::less<T>>
std::vector<long long> longest_increasing_subsequence_lengths(const std::vector<T>& a, bool strict = true,
                                                              Compare comp = Compare()) {
    // tail[k] は、長さ k + 1 の増加部分列の末尾として最も小さい値
    std::vector<T> tail;
    std::vector<long long> res(a.size());
    for (long long i = 0; i < static_cast<long long>(a.size()); i++) {
        auto it = strict ? std::lower_bound(tail.begin(), tail.end(), a[i], comp)
                         : std::upper_bound(tail.begin(), tail.end(), a[i], comp);
        res[i] = it - tail.begin() + 1;
        if (it == tail.end())
            tail.push_back(a[i]);
        else
            *it = a[i];
    }
    return res;
}

/**
 * @brief 最長増加部分列の長さ O(n log n)
 * @param strict true なら狭義単調増加 (comp(前, 後) が成り立つ)、false なら広義単調増加 (comp(後, 前) が成り立たない)
 * @param comp 大小の比較。std::greater<T>() を渡すと減少列になる
 */
template <typename T, typename Compare = std::less<T>>
long long longest_increasing_subsequence(const std::vector<T>& a, bool strict = true, Compare comp = Compare()) {
    // tail[k] は、長さ k + 1 の増加部分列の末尾として最も小さい値
    std::vector<T> tail;
    for (const T& x : a) {
        auto it = strict ? std::lower_bound(tail.begin(), tail.end(), x, comp)
                         : std::upper_bound(tail.begin(), tail.end(), x, comp);
        if (it == tail.end())
            tail.push_back(x);
        else
            *it = x;
    }
    return tail.size();
}

/**
 * @brief 最長増加部分列の復元の結果
 *
 * auto [length, indices] = longest_increasing_subsequence_restore(a); のように受け取れる
 */
struct LongestIncreasingSubsequence {
    long long length;                //!< 長さ
    std::vector<long long> indices;  //!< 選んだ添字の昇順のリスト
};

/**
 * @brief 最長増加部分列を 1 つ求め、長さと添字の昇順のリストを返す O(n log n)
 * @param strict true なら狭義単調増加 (comp(前, 後) が成り立つ)、false なら広義単調増加 (comp(後, 前) が成り立たない)
 * @param comp 大小の比較。std::greater<T>() を渡すと減少列になる
 */
template <typename T, typename Compare = std::less<T>>
LongestIncreasingSubsequence longest_increasing_subsequence_restore(const std::vector<T>& a, bool strict = true,
                                                                    Compare comp = Compare()) {
    std::vector<long long> len = longest_increasing_subsequence_lengths(a, strict, comp);
    long long k = len.empty() ? 0 : *std::max_element(len.begin(), len.end());
    // 後ろから、長さが 1 ずつ減り、値が条件を満たす添字を選ぶ
    const long long length = k;
    std::vector<long long> res(k);
    long long last = -1;
    for (long long i = static_cast<long long>(a.size()) - 1; i >= 0 && k > 0; i--) {
        if (len[i] != k) continue;
        if (last != -1 && !(strict ? comp(a[i], a[last]) : !comp(a[last], a[i]))) continue;
        res[--k] = i;
        last = i;
    }
    return {length, res};
}
#line 7 "test/unittest/dp/longest-increasing-subsequence.test.cpp"

#line 10 "test/unittest/dp/longest-increasing-subsequence.test.cpp"
#include <iostream>
#include <random>
#line 13 "test/unittest/dp/longest-increasing-subsequence.test.cpp"

template <typename Compare>
bool check(const std::vector<int>& a, bool strict, Compare comp) {
    int n = a.size();
    auto ok_pair = [&](long long i, long long j) { return strict ? comp(a[i], a[j]) : !comp(a[j], a[i]); };
    // 愚直に a[i] で終わる最長の長さを求める
    std::vector<long long> len(n);
    for (int j = 0; j < n; j++) {
        len[j] = 1;
        for (int i = 0; i < j; i++) {
            if (ok_pair(i, j)) len[j] = std::max(len[j], len[i] + 1);
        }
    }
    long long best = n == 0 ? 0 : *std::max_element(len.begin(), len.end());
    if (longest_increasing_subsequence_lengths(a, strict, comp) != len) return false;
    if (longest_increasing_subsequence(a, strict, comp) != best) return false;
    auto [length, idx] = longest_increasing_subsequence_restore(a, strict, comp);
    if (length != best || static_cast<long long>(idx.size()) != best) return false;
    for (long long k = 0; k < static_cast<long long>(idx.size()); k++) {
        if (idx[k] < 0 || idx[k] >= n) return false;
        if (k > 0 && (idx[k - 1] >= idx[k] || !ok_pair(idx[k - 1], idx[k]))) return false;
    }
    return true;
}

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 3000 && ok; rep++) {
        int n = rng() % 15;
        int range = rng() % 10 + 1;
        std::vector<int> a(n);
        for (auto& v : a) v = rng() % range;
        for (bool strict : {true, false}) {
            ok = ok && check(a, strict, std::less<int>());
            ok = ok && check(a, strict, std::greater<int>());
        }
    }

    // 例
    std::vector<int> a = {3, 1, 4, 1, 5, 9, 2, 6};
    ok = ok && longest_increasing_subsequence(a) == 4;
    ok = ok && longest_increasing_subsequence(a, false) == 4;
    ok = ok && longest_increasing_subsequence(std::vector<int>{2, 2, 2}, false) == 3;
    ok = ok && longest_increasing_subsequence(std::vector<int>{2, 2, 2}) == 1;
    ok = ok && longest_increasing_subsequence(a, true, std::greater<int>()) == 2;

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