結果
問題 |
No.1368 サイクルの中に眠る門松列
|
ユーザー |
![]() |
提出日時 | 2021-02-04 09:52:29 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 129 ms / 2,000 ms |
コード長 | 1,443 bytes |
コンパイル時間 | 901 ms |
コンパイル使用メモリ | 112,232 KB |
実行使用メモリ | 54,316 KB |
最終ジャッジ日時 | 2024-06-30 15:02:53 |
合計ジャッジ時間 | 3,258 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 15 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using static System.Math; using System; public class Hello { public static int[] a, b, c; static void Main() { var q = int.Parse(Console.ReadLine().Trim()); while (q-- > 0) { var n = int.Parse(Console.ReadLine().Trim()); string[] line = Console.ReadLine().Trim().Split(' '); a = new int[n]; b = new int[n]; c = new int[n]; for (int i = 0; i < n; i++) { var t = int.Parse(line[i]); a[i] = t; if (i - 1 >= 0) b[i - 1] = t; if (i - 2 >= 0) c[i - 2] = t; } b[n - 1] = a[0]; c[n - 2] = a[0]; c[n - 1] = a[1]; var ans = Max(Dp(n, a), Dp(n, b)); ans = Max(ans, Dp(n, c)); Console.WriteLine(ans); } } static long Dp(int n, int[] t) { var dp = new long[n]; dp[2] = check(t[0], t[1], t[2]) ? t[0] : 0; for (int i = 3; i < n; i++) { if (check(t[i - 2], t[i - 1], t[i])) dp[i] = Max(dp[i - 1], dp[i - 3] + t[i - 2]); else dp[i] = dp[i - 1]; } return dp[n - 1]; } static bool check(int t0, int t1, int t2) { if (!(t0 != t1 && t1 != t2 && t0 != t2)) return false; if (t1 > t0 && t1 > t2) return true; if (t1 < t0 && t1 < t2) return true; return false; } }