結果

問題 No.1460 Max of Min
ユーザー KoDKoD
提出日時 2021-03-31 22:26:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 685 ms / 2,000 ms
コード長 3,798 bytes
コンパイル時間 2,636 ms
コンパイル使用メモリ 209,004 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-05-09 08:37:51
合計ジャッジ時間 31,241 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 487 ms
11,132 KB
testcase_04 AC 499 ms
11,136 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 482 ms
11,136 KB
testcase_07 AC 496 ms
11,136 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 663 ms
11,008 KB
testcase_11 AC 611 ms
11,008 KB
testcase_12 AC 70 ms
5,376 KB
testcase_13 AC 445 ms
10,368 KB
testcase_14 AC 111 ms
5,760 KB
testcase_15 AC 47 ms
5,376 KB
testcase_16 AC 302 ms
9,856 KB
testcase_17 AC 182 ms
7,168 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 154 ms
6,640 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 19 ms
5,376 KB
testcase_22 AC 158 ms
6,912 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 203 ms
7,680 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 383 ms
10,872 KB
testcase_27 AC 49 ms
5,376 KB
testcase_28 AC 14 ms
5,376 KB
testcase_29 AC 184 ms
7,424 KB
testcase_30 AC 347 ms
10,368 KB
testcase_31 AC 167 ms
7,028 KB
testcase_32 AC 260 ms
9,464 KB
testcase_33 AC 85 ms
5,376 KB
testcase_34 AC 121 ms
6,400 KB
testcase_35 AC 41 ms
5,376 KB
testcase_36 AC 14 ms
5,376 KB
testcase_37 AC 160 ms
7,168 KB
testcase_38 AC 318 ms
9,728 KB
testcase_39 AC 93 ms
5,632 KB
testcase_40 AC 65 ms
5,376 KB
testcase_41 AC 46 ms
5,376 KB
testcase_42 AC 24 ms
5,376 KB
testcase_43 AC 149 ms
6,784 KB
testcase_44 AC 250 ms
9,208 KB
testcase_45 AC 8 ms
5,376 KB
testcase_46 AC 111 ms
5,888 KB
testcase_47 AC 154 ms
6,656 KB
testcase_48 AC 343 ms
9,728 KB
testcase_49 AC 213 ms
8,064 KB
testcase_50 AC 11 ms
5,376 KB
testcase_51 AC 155 ms
7,160 KB
testcase_52 AC 33 ms
5,376 KB
testcase_53 AC 170 ms
7,424 KB
testcase_54 AC 393 ms
11,008 KB
testcase_55 AC 428 ms
11,008 KB
testcase_56 AC 417 ms
11,136 KB
testcase_57 AC 422 ms
11,132 KB
testcase_58 AC 614 ms
11,136 KB
testcase_59 AC 417 ms
11,008 KB
testcase_60 AC 382 ms
11,124 KB
testcase_61 AC 376 ms
11,008 KB
testcase_62 AC 371 ms
11,008 KB
testcase_63 AC 349 ms
11,136 KB
testcase_64 AC 371 ms
11,008 KB
testcase_65 AC 390 ms
11,136 KB
testcase_66 AC 364 ms
11,136 KB
testcase_67 AC 372 ms
11,132 KB
testcase_68 AC 419 ms
11,136 KB
testcase_69 AC 405 ms
11,136 KB
testcase_70 AC 403 ms
11,008 KB
testcase_71 AC 396 ms
11,124 KB
testcase_72 AC 366 ms
11,136 KB
testcase_73 AC 365 ms
11,136 KB
testcase_74 AC 387 ms
11,008 KB
testcase_75 AC 469 ms
11,128 KB
testcase_76 AC 362 ms
11,000 KB
testcase_77 AC 383 ms
11,008 KB
testcase_78 AC 328 ms
11,008 KB
testcase_79 AC 389 ms
11,008 KB
testcase_80 AC 407 ms
10,880 KB
testcase_81 AC 365 ms
11,000 KB
testcase_82 AC 327 ms
10,880 KB
testcase_83 AC 369 ms
10,880 KB
testcase_84 AC 484 ms
10,752 KB
testcase_85 AC 656 ms
10,752 KB
testcase_86 AC 685 ms
10,880 KB
testcase_87 AC 546 ms
11,008 KB
testcase_88 AC 521 ms
10,880 KB
testcase_89 AC 514 ms
10,880 KB
testcase_90 AC 459 ms
10,880 KB
testcase_91 AC 621 ms
11,136 KB
testcase_92 AC 600 ms
11,136 KB
testcase_93 AC 674 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept: itr(pos) { }
        constexpr void operator ++ () noexcept { ++itr; }
        constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator * () const noexcept { return itr; }
    };
    const Iter first, last;
public:
    explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { }
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

class revrep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept: itr(pos) { }
        constexpr void operator ++ () noexcept { --itr; }
        constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator * () const noexcept { return itr; }
    };
    const Iter first, last;
public:
    explicit constexpr revrep(const usize first, const usize last) noexcept: first(last - 1), last(std::min(first, last) - 1) { }
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T, T Div = 2>
constexpr T INFTY = std::numeric_limits<T>::max() / Div;

template <class T>
using Vec = std::vector<T>;

struct SemiRing {
    static constexpr SemiRing zero() {
        return SemiRing { -INFTY<i64> };
    }
    static constexpr SemiRing one() {
        return SemiRing { INFTY<i64> };
    }
    i64 x;
    constexpr SemiRing operator + (const SemiRing& other) const {
        return SemiRing { std::max(x, other.x) };
    }
    constexpr SemiRing operator * (const SemiRing& other) const {
        return SemiRing { std::min(x, other.x) };
    }
};

void H_main() {
    // O(K^2 log N) ?
    // + ... max, x ... min の半環
    // 中一でこんなの知ってるのか...
    // kitamasa法 書いたことないんだが
    // A[n+k] = B[0] * A[n] + B[1] * A[n+1] + ... + B[k-1] * A[n+k-1]
    usize K;
    u64 N;
    std::cin >> K >> N;
    Vec<SemiRing> A(K), B(K);
    for (auto& x: A) {
        std::cin >> x.x;
    }
    for (auto& x: B) {
        std::cin >> x.x;
    }
    const auto next = [&](const Vec<SemiRing>& vec) {
        Vec<SemiRing> ret(K, SemiRing::zero());
        ret[0] = vec[K - 1] * B[0];
        for (auto i: rep(0, K - 1)) {
            ret[i + 1] = vec[i] + vec[K - 1] * B[i + 1];
        }
        return ret;
    };
    const auto dbl = [&](const Vec<SemiRing>& vec) {
        Vec<Vec<SemiRing>> coeff(K, Vec<SemiRing>(K, SemiRing::zero()));
        coeff[0] = vec;
        for (auto i: rep(0, K - 1)) {
            coeff[i + 1] = next(coeff[i]);
        }
        Vec<SemiRing> ret(K, SemiRing::zero());
        for (auto i: rep(0, K)) {
            for (auto j: rep(0, K)) {
                ret[i] = ret[i] + coeff[0][j] * coeff[j][i];
            }
        }
        return ret;
    };
    // C(K, *) = B
    // C(0, *) = { 1, 0, 0, ..., 0 }
    Vec<SemiRing> coeff(K, SemiRing::zero());
    coeff[0] = SemiRing::one();
    for (auto d: revrep(0, 64)) {
        coeff = dbl(coeff);
        if (N >> d & 1) {
            coeff = next(coeff);
        }
    }
    SemiRing ans = SemiRing::zero();
    for (auto i: rep(0, K)) {
        ans = ans + A[i] * coeff[i];
    }
    std::cout << ans.x << '\n';
    return;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    H_main(); 
    return 0;
}
0