結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー sten_sansten_san
提出日時 2021-01-29 22:33:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,705 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 202,764 KB
実行使用メモリ 8,820 KB
最終ジャッジ日時 2023-09-09 16:05:01
合計ジャッジ時間 3,818 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 44 ms
4,380 KB
testcase_03 AC 10 ms
4,380 KB
testcase_04 AC 16 ms
4,380 KB
testcase_05 AC 69 ms
8,556 KB
testcase_06 AC 70 ms
8,500 KB
testcase_07 AC 69 ms
8,556 KB
testcase_08 AC 70 ms
8,568 KB
testcase_09 AC 54 ms
8,504 KB
testcase_10 AC 54 ms
8,588 KB
testcase_11 AC 54 ms
8,596 KB
testcase_12 AC 54 ms
8,536 KB
testcase_13 AC 54 ms
8,544 KB
testcase_14 AC 54 ms
8,556 KB
testcase_15 AC 54 ms
8,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

int main() {
    auto kadomatsu = [](int a, int b, int c) {
        return a != b && a != c && b != c && (max({ a, b, c }) == b || min({ a, b, c}) == b);
    };


    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        auto a = vec<int>(uns, n);
        for (auto &e : a) cin >> e;

        auto mod = [n](auto a) {
            return (n + a % n) % n;
        };

        auto dp1 = vec<int64_t>(uns, n + 1);
        auto dp2 = vec<int64_t>(uns, n + 1);
        auto dp3 = vec<int64_t>(uns, n + 1);
        dp1[0] = dp1[1] = dp1[2] = 0;
        dp2[0] = dp2[1] = dp2[2] = 0;
        dp3[0] = dp3[1] = dp3[2] = 0;

        for (int i = 3; i <= n; ++i) {
            dp1[i] = max(
                dp1[i - 1],
                dp1[i - 3] + a[mod(i - 3)] * kadomatsu(a[mod(i - 3)], a[mod(i - 2)], a[mod(i - 1)])
            );
            dp2[i] = max(
                dp2[i - 1],
                dp2[i - 3] + a[mod(i - 4)] * kadomatsu(a[mod(i - 4)], a[mod(i - 3)], a[mod(i - 2)])
            );
            dp3[i] = max(
                dp3[i - 1],
                dp3[i - 3] + a[mod(i - 5)] * kadomatsu(a[mod(i - 5)], a[mod(i - 4)], a[mod(i - 3)])
            );
        }

        cout << max({ dp1[n], dp2[n], dp3[n] }) << endl;
    }
}

0