結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー tnakao0123tnakao0123
提出日時 2021-01-30 17:57:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,308 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 91,852 KB
実行使用メモリ 5,936 KB
最終ジャッジ日時 2023-09-10 14:26:43
合計ジャッジ時間 7,071 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,036 KB
testcase_01 AC 4 ms
5,000 KB
testcase_02 TLE -
testcase_03 AC 453 ms
5,104 KB
testcase_04 AC 719 ms
5,272 KB
testcase_05 AC 27 ms
5,932 KB
testcase_06 AC 26 ms
5,936 KB
testcase_07 AC 25 ms
5,876 KB
testcase_08 AC 26 ms
5,932 KB
testcase_09 AC 27 ms
5,840 KB
testcase_10 AC 27 ms
5,780 KB
testcase_11 AC 27 ms
5,784 KB
testcase_12 AC 27 ms
5,880 KB
testcase_13 AC 27 ms
5,776 KB
testcase_14 AC 27 ms
5,780 KB
testcase_15 AC 27 ms
5,780 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++) {
      memset(dp, 0, sizeof(dp));
      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