結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー hongrockhongrock
提出日時 2021-01-29 21:37:28
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,255 bytes
コンパイル時間 1,514 ms
コンパイル使用メモリ 166,112 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-06-27 07:48:28
合計ジャッジ時間 2,571 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1

using LL = long long;
using ULL = unsigned long long;
using pii = pair<int,int>;
using PLL = pair<LL, LL>;
using UI = unsigned int;

const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1.0);

const int N = 2e5 + 10;

int T, n, a[N];
LL dp[N];

bool ok(int p){
	if(a[p-1] == a[p+1])	return 0;
	if(a[p-1] > a[p] && a[p] < a[p+1])	return 1;
	if(a[p-1] < a[p] && a[p] > a[p+1])	return 1;
	return 0;
}

LL cal(int st){
	rep(i, 0, n + 1){
		dp[i] = 0;	
	}
	rep(i, 0, n){
		dp[i+1] = max(dp[i+1], dp[i]);
		if(ok(st + i + 1)){
			dp[i+3] = max(dp[i+3], dp[i] + a[st + i]);
		}
	}
	return dp[n];
}

int main(){
	scanf("%d", &T);
	while(T--){
		scanf("%d", &n);
		rep(i, 0, n)	scanf("%d", a+i);
		a[n] = a[0];
		a[n+1] = a[1];

		LL ans = 0;
		rep(i, 0, 3)	ans = max(ans, cal(i));
		printf("%lld\n", ans);
	}
	return 0;
}
0