結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー MisterMister
提出日時 2021-01-29 21:31:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 998 bytes
コンパイル時間 635 ms
コンパイル使用メモリ 77,280 KB
実行使用メモリ 6,280 KB
最終ジャッジ日時 2023-09-09 14:46:47
合計ジャッジ時間 1,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 12 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 24 ms
6,148 KB
testcase_06 AC 24 ms
6,228 KB
testcase_07 AC 24 ms
6,152 KB
testcase_08 AC 24 ms
6,156 KB
testcase_09 AC 23 ms
6,196 KB
testcase_10 AC 23 ms
6,192 KB
testcase_11 AC 23 ms
6,204 KB
testcase_12 AC 23 ms
6,280 KB
testcase_13 AC 23 ms
6,252 KB
testcase_14 AC 23 ms
6,188 KB
testcase_15 AC 23 ms
6,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <iostream>
#include <vector>

using namespace std;

using lint = long long;

bool judge(int a, int b, int c) {
    if (a == c) return false;
    return (a < b && b > c) ||
           (a > b && b < c);
}

constexpr lint INF = 1LL << 50;

void solve() {
    int n;
    cin >> n;
    vector<int> xs(n);
    for (auto& x : xs) cin >> x;

    lint ans = -INF;
    for (int q = 0; q < 3; ++q) {
        vector<lint> dp(n + 1, -INF);
        dp[0] = 0;

        for (int i = 0; i < n; ++i) {
            dp[i + 1] = max(dp[i + 1], dp[i]);
            if (i + 2 < n &&
                judge(xs[i], xs[i + 1], xs[i + 2])) {
                dp[i + 3] = max(dp[i + 3], dp[i] + xs[i]);
            }
        }
        ans = max(ans, dp.back());

        xs.insert(xs.begin(), xs.back());
        xs.pop_back();
    }

    cout << ans << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int q;
    cin >> q;
    while (q--) solve();

    return 0;
}
0