結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー KKT89KKT89
提出日時 2021-02-01 15:03:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,228 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 204,716 KB
実行使用メモリ 6,368 KB
最終ジャッジ日時 2023-09-12 10:10:13
合計ジャッジ時間 4,038 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 13 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 27 ms
6,288 KB
testcase_06 AC 26 ms
6,280 KB
testcase_07 AC 26 ms
6,224 KB
testcase_08 AC 27 ms
6,368 KB
testcase_09 AC 27 ms
6,156 KB
testcase_10 AC 27 ms
6,204 KB
testcase_11 AC 26 ms
6,156 KB
testcase_12 AC 27 ms
6,320 KB
testcase_13 AC 27 ms
6,164 KB
testcase_14 AC 26 ms
6,220 KB
testcase_15 AC 26 ms
6,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
 
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        int n; cin >> n;
        vector<ll> a(n);
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        ll res=0;
        auto ok=[&](int i)->bool{
            if(i-1<0 or i+1>=n)return false;
            if(a[i]==a[i-1] or a[i-1]==a[i+1] or a[i+1]==a[i])return false;
            if(a[i]>a[i-1] and a[i]>a[i+1])return true;
            if(a[i]<a[i-1] and a[i]<a[i+1])return true;
            return false;
        };
        for(int _=0;_<3;_++){
            vector<ll> dp(n+1);
            for(int i=0;i<n;i++){
                if(ok(i)){
                    dp[i+2]=max(dp[i+2],dp[i-1]+a[i-1]);
                }
                if(i){
                    dp[i+1]=max(dp[i+1],dp[i]);
                }
            }
            res=max(res,*max_element(dp.begin(), dp.end()));
            rotate(a.begin(), a.begin()+1, a.end());
        }
        cout << res << "\n";
    }
}
0