結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー tnakao0123tnakao0123
提出日時 2021-01-30 17:58:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 936 ms
コンパイル使用メモリ 92,132 KB
実行使用メモリ 5,996 KB
最終ジャッジ日時 2023-09-10 14:28:19
合計ジャッジ時間 2,690 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 12 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 26 ms
5,860 KB
testcase_06 AC 27 ms
5,872 KB
testcase_07 AC 26 ms
5,732 KB
testcase_08 AC 26 ms
5,796 KB
testcase_09 AC 26 ms
5,996 KB
testcase_10 AC 27 ms
5,872 KB
testcase_11 AC 28 ms
5,808 KB
testcase_12 AC 27 ms
5,812 KB
testcase_13 AC 27 ms
5,812 KB
testcase_14 AC 27 ms
5,812 KB
testcase_15 AC 27 ms
5,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1368.cc:  No.1368 サイクルの中に眠る門松列 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N + 2];
ll dp[MAX_N + 1];

/* subroutines */

bool kadomatsu(int v[]) {
  return
    v[0] != v[2] &&
    ((v[0] < v[1] && v[1] > v[2]) || (v[0] > v[1] && v[1] < v[2]));
}

inline void setmax(ll &a, ll b) { if (a < b) a = b; }

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) scanf("%d", as + i);
    as[n] = as[0], as[n + 1] = as[1];

    ll maxsum = 0;
    for (int st = 0; st < 3; st++) {
      fill(dp, dp + n + 1, 0);
      for (int i = 0, j = st; i < n; i++, j++) {
	setmax(dp[i + 1], dp[i]);
	if (i + 3 <= n && kadomatsu(as + j))
	  setmax(dp[i + 3], dp[i] + as[j]);
      }
      setmax(maxsum, dp[n]);
    }

    printf("%lld\n", maxsum);
  }
  return 0;
}
0