結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー bluemeganebluemegane
提出日時 2021-02-04 09:52:29
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 1,443 bytes
コンパイル時間 1,649 ms
コンパイル使用メモリ 63,392 KB
実行使用メモリ 48,776 KB
最終ジャッジ日時 2023-09-13 04:50:10
合計ジャッジ時間 3,971 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,680 KB
testcase_01 AC 58 ms
20,736 KB
testcase_02 AC 152 ms
24,772 KB
testcase_03 AC 75 ms
24,880 KB
testcase_04 AC 89 ms
26,924 KB
testcase_05 AC 146 ms
48,776 KB
testcase_06 AC 145 ms
46,896 KB
testcase_07 AC 143 ms
46,712 KB
testcase_08 AC 147 ms
48,764 KB
testcase_09 AC 140 ms
45,320 KB
testcase_10 AC 140 ms
45,344 KB
testcase_11 AC 140 ms
43,272 KB
testcase_12 AC 140 ms
45,280 KB
testcase_13 AC 141 ms
45,280 KB
testcase_14 AC 141 ms
45,304 KB
testcase_15 AC 141 ms
43,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }
}
0