結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー さかぽんさかぽん
提出日時 2021-02-18 15:21:13
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,071 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 105,784 KB
実行使用メモリ 49,760 KB
最終ジャッジ日時 2023-10-12 23:32:20
合計ジャッジ時間 5,755 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
21,616 KB
testcase_01 AC 66 ms
21,732 KB
testcase_02 AC 123 ms
25,724 KB
testcase_03 AC 77 ms
23,600 KB
testcase_04 AC 88 ms
27,772 KB
testcase_05 AC 178 ms
49,752 KB
testcase_06 AC 178 ms
49,760 KB
testcase_07 AC 173 ms
46,280 KB
testcase_08 AC 181 ms
45,832 KB
testcase_09 AC 170 ms
43,424 KB
testcase_10 AC 169 ms
43,460 KB
testcase_11 AC 171 ms
45,536 KB
testcase_12 AC 173 ms
43,420 KB
testcase_13 AC 171 ms
43,412 KB
testcase_14 AC 172 ms
43,608 KB
testcase_15 AC 174 ms
45,660 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Linq;

class C
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static void Main() => Console.WriteLine(string.Join("\n", new int[int.Parse(Console.ReadLine())].Select(_ => Solve())));
	static object Solve()
	{
		var n = int.Parse(Console.ReadLine());
		var a = Read().ToList();

		var r = 0L;
		for (int i = 0; i < 3; i++)
		{
			r = Math.Max(r, GetMax(a.ToArray()));

			a.Add(a[0]);
			a.RemoveAt(0);
		}
		return r;
	}

	static long GetMax(int[] a)
	{
		var p = new long[a.Length];
		for (int i = 0; i < a.Length - 2; i++)
			if (IsKadomatsu(a, i)) p[i] = a[i];

		var dp = new long[a.Length];
		for (int i = 0; i < a.Length; i++)
		{
			var max = 0L;
			if (i - 5 >= 0) max = Math.Max(max, dp[i - 5]);
			if (i - 4 >= 0) max = Math.Max(max, dp[i - 4]);
			if (i - 3 >= 0) max = Math.Max(max, dp[i - 3]);
			dp[i] = max + p[i];
		}
		return dp.Max();
	}

	static bool IsKadomatsu(int[] a, int i) => a[i] != a[i + 2] && (a[i] < a[i + 1] && a[i + 1] > a[i + 2] || a[i] > a[i + 1] && a[i + 1] < a[i + 2]);
}
0