結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー umezoumezo
提出日時 2021-01-29 22:47:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,490 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 203,724 KB
実行使用メモリ 17,336 KB
最終ジャッジ日時 2023-09-09 16:25:32
合計ジャッジ時間 3,737 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 45 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 16 ms
4,380 KB
testcase_05 AC 74 ms
17,104 KB
testcase_06 AC 74 ms
17,336 KB
testcase_07 AC 73 ms
17,020 KB
testcase_08 AC 73 ms
17,020 KB
testcase_09 AC 58 ms
16,972 KB
testcase_10 AC 57 ms
17,020 KB
testcase_11 AC 57 ms
17,300 KB
testcase_12 AC 57 ms
17,024 KB
testcase_13 AC 57 ms
16,980 KB
testcase_14 AC 57 ms
17,084 KB
testcase_15 AC 57 ms
16,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

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

bool iskado(ll x,ll y,ll z){
  if(y>x && y>z && x!=z) return true;
  if(y<x && y<z && x!=z) return true;
  return false;
}

int main(){
  int t;
  cin>>t;
  
  while(t--){
    int n;
    cin>>n;
    
    vector<ll> A(n),B(n),C(n);
    rep(i,n){
      ll x;
      cin>>x;
      A[i]=x;
      B[(i+1)%n]=x;
      C[(i+2)%n]=x;
    }
    
    vector<ll> dpa(n),dpb(n),dpc(n);
    vector<ll> maa(n),mab(n),mac(n);
    
    for(int i=0;i<n-2;i++){
      if(iskado(A[i],A[i+1],A[i+2])==false){
        if(i>0) maa[i]=maa[i-1];
        continue;
      }
      if(i<3) dpa[i]=A[i];
      else dpa[i]=maa[i-3]+A[i];
      if(i==0) maa[i]=dpa[i];
      else maa[i]=max(maa[i-1],dpa[i]);
    }
    for(int i=0;i<n-2;i++){
      if(iskado(B[i],B[i+1],B[i+2])==false){
        if(i>0) mab[i]=mab[i-1];
        continue;
      }
      if(i<3) dpb[i]=B[i];
      else dpb[i]=mab[i-3]+B[i];
      if(i==0) mab[i]=dpb[i];
      else mab[i]=max(mab[i-1],dpb[i]);
    }
    for(int i=0;i<n-2;i++){
      if(iskado(C[i],C[i+1],C[i+2])==false){
        if(i>0) mac[i]=mac[i-1];
        continue;
      }
      if(i<3) dpc[i]=C[i];
      else dpc[i]=mac[i-3]+C[i];
      if(i==0) mac[i]=dpc[i];
      else mac[i]=max(mac[i-1],dpc[i]);
    }
    
    ll ans=0;
    rep(i,n-2){
      ans=max({ans,dpa[i],dpb[i],dpc[i]});
    }
    cout<<ans<<endl;
  }

  return 0;
}
0