結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2021-01-29 21:44:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,352 bytes
コンパイル時間 2,121 ms
コンパイル使用メモリ 207,564 KB
実行使用メモリ 21,980 KB
最終ジャッジ日時 2023-09-09 15:05:58
合計ジャッジ時間 3,720 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 40 ms
4,380 KB
testcase_03 AC 8 ms
4,376 KB
testcase_04 AC 13 ms
4,380 KB
testcase_05 AC 45 ms
21,980 KB
testcase_06 AC 45 ms
21,972 KB
testcase_07 AC 46 ms
21,880 KB
testcase_08 AC 47 ms
21,788 KB
testcase_09 AC 44 ms
21,908 KB
testcase_10 AC 44 ms
21,972 KB
testcase_11 AC 45 ms
21,964 KB
testcase_12 AC 45 ms
21,788 KB
testcase_13 AC 44 ms
21,880 KB
testcase_14 AC 44 ms
21,880 KB
testcase_15 AC 44 ms
21,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
using ld = long double;
#define all(s) (s).begin(),(s).end()
#define vcin(n) for(ll i=0;i<ll(n.size());i++) cin>>n[i]
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define se second
#define SIZE(n) int(n.size())
#define P pair<ll,ll>
//const ll mod = 998244353;
const ll mod = 1000000007;
const ll inf = 2000000000000000000ll;
static const long double pi = 3.141592653589793;
template<class T,class U> void chmax(T& t,const U& u){if(t<u) t=u;}
template<class T,class U> void chmin(T& t,const U& u){if(t>u) t=u;}
ll modPow(ll a, ll n, ll modulo) { ll ret = 1; ll p = a % modulo; while (n) { if (n & 1) ret = ret * p % modulo; p = p * p % modulo; n >>= 1; } return ret; }
bool check(ll a,ll b,ll c){
  if((a-b)*(c-b)>0&&a!=c&&b!=c&&a!=b){
    return true;
  }
  return false;
}
void solve(){
  ll n;
  cin>>n;
  vector<ll> a(n);
  vcin(a);
  vector<bool> t(n);
  vector<ll> b(2*n);
  for(int i=0;i<n;i++){
    b[i]=a[i];
    b[i+n]=a[i];
  }
  for(int i=0;i<n;i++){
    t[i]=check(b[i],b[i+1],b[i+2]);
  }
  vector<vector<ll>> dp(n+3,vector<ll>(4));
  vector<ll> m(4);
  for(int i=0;i<n;i++){
    if(i>=2){
      for(int j=0;j<4;j++){
        if(t[i]){
          dp[i][j]=m[j]+a[i];
        }
        else{
          dp[i][j]=m[j];
        }
      }
    }
    else{
      if(i==0){
        for(int j=0;j<4;j++){
          if(t[i]&&j<=1){
            dp[i][j]=m[j]+a[i];
          }
          else{
            dp[i][j]=m[j];
          }
        }
      }
      if(i==1){
        for(int j=0;j<4;j++){
          if(t[i]&&j%2==0){
            dp[i][j]=m[j]+a[i];
          }
          else{
            dp[i][j]=m[j];
          }
        }
      }
    }
    if(i>=2){
      for(int j=0;j<4;j++){
        chmax(m[j],dp[i-2][j]);
      }
    }
  }
  ll ans=0;
  for(int i=0;i<n;i++){
    for(int j=0;j<4;j++){
   //   cout<<dp[i][j]<<" ";
      if((i==n-2&&(j==0||j==1))||(i==n-1&&j!=3)){
        continue;
      }
      chmax(ans,dp[i][j]);
    }
  //  cout<<endl;
  }
  cout<<ans<<endl;
}
int main() {
  /* mod は 1e9+7 */
  ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  cout<< fixed << setprecision(10);
  ll a;
  cin>>a;
  while(a--){
    solve();
  }
}
0