結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー boutarouboutarou
提出日時 2021-01-29 21:57:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 970 bytes
コンパイル時間 2,333 ms
コンパイル使用メモリ 203,732 KB
実行使用メモリ 8,504 KB
最終ジャッジ日時 2023-09-09 15:20:46
合計ジャッジ時間 3,771 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 50 ms
4,380 KB
testcase_03 AC 10 ms
4,380 KB
testcase_04 AC 19 ms
4,376 KB
testcase_05 AC 78 ms
8,328 KB
testcase_06 AC 77 ms
8,276 KB
testcase_07 AC 76 ms
8,200 KB
testcase_08 AC 77 ms
8,304 KB
testcase_09 AC 60 ms
8,188 KB
testcase_10 AC 60 ms
8,336 KB
testcase_11 AC 61 ms
8,280 KB
testcase_12 AC 59 ms
8,504 KB
testcase_13 AC 60 ms
8,224 KB
testcase_14 AC 60 ms
8,224 KB
testcase_15 AC 60 ms
8,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i = 0; i < ll(n); i++)
using ll = long long;
using P = pair<ll, ll>;

bool kadomatsu(ll a, ll b, ll c) {
    if (a != b && b != c && c != a && (max({a, b, c}) == b || min({a, b, c}) == b)) return true;
    return false;
}
void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) {
        cin >> a[i];
    }
    ll ans = 0;
    rep(i, 3) {
        vector<ll> b;
        rep(j, n) {
            b.push_back(a[(i + j) % n]);
        }
        vector<ll> dp(n + 1);
        rep(j, n + 1) {
            if (j != 0) {
                dp[j] = max(dp[j - 1], dp[j]);
            }
            if (j < n - 2 && kadomatsu(b[j], b[j + 1], b[j + 2])) {
                dp[j + 3] = max(dp[j + 3], dp[j] + b[j]);
            }
        }
        ans = max(ans, dp[n]);
    }
    cout << ans << endl;
}

int main() {
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
0