結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー ocvret_ocvret_
提出日時 2021-01-29 22:10:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 956 bytes
コンパイル時間 1,729 ms
コンパイル使用メモリ 171,180 KB
実行使用メモリ 5,392 KB
最終ジャッジ日時 2023-09-09 15:34:54
合計ジャッジ時間 3,313 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 42 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 15 ms
4,380 KB
testcase_05 AC 63 ms
5,164 KB
testcase_06 AC 63 ms
5,196 KB
testcase_07 AC 62 ms
5,332 KB
testcase_08 AC 64 ms
5,260 KB
testcase_09 AC 48 ms
5,264 KB
testcase_10 AC 48 ms
5,152 KB
testcase_11 AC 48 ms
5,392 KB
testcase_12 AC 48 ms
5,236 KB
testcase_13 AC 48 ms
5,268 KB
testcase_14 AC 48 ms
5,264 KB
testcase_15 AC 48 ms
5,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>

using namespace std;
// using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007


int main(){
  
  int T;
  cin >> T;
  while(T--){
    int n;cin >> n;
    vector<int> a(n);
    rep(i,n)cin >> a[i];
    rep(i,3)a.push_back(a[i]);
    auto solve = [&](int s){
      vector<ll> dp(n+10);
      for(int i = s+2;i < n+s;i++){
        dp[i] = max(dp[i],dp[i-1]);
        ll k = 0;
        if(i-3 >= 0)k = dp[i-3];
        if(a[i-2] != a[i-1] && a[i-1] != a[i] && a[i-2] != a[i] && (*min_element(a.begin()+i-2,a.begin()+i+1) == a[i-1] || *max_element(a.begin()+i-2,a.begin()+i+1) == a[i-1])){
          dp[i] = max(dp[i],k + a[i-2]);
        }
      }
      return dp[n+s-1];
    };
    cout << max({solve(0),solve(1),solve(2)}) << "\n";
  }
  

  return 0;
}
0