結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー bluemeganebluemegane
提出日時 2021-02-04 09:45:18
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,504 bytes
コンパイル時間 1,872 ms
コンパイル使用メモリ 63,544 KB
実行使用メモリ 46,412 KB
最終ジャッジ日時 2023-09-13 04:44:46
合計ジャッジ時間 4,149 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,556 KB
testcase_01 AC 57 ms
20,656 KB
testcase_02 AC 157 ms
24,736 KB
testcase_03 AC 77 ms
26,800 KB
testcase_04 AC 90 ms
26,776 KB
testcase_05 AC 160 ms
46,412 KB
testcase_06 AC 160 ms
44,292 KB
testcase_07 AC 157 ms
46,344 KB
testcase_08 AC 161 ms
44,320 KB
testcase_09 AC 154 ms
40,116 KB
testcase_10 AC 154 ms
42,216 KB
testcase_11 AC 153 ms
40,216 KB
testcase_12 AC 154 ms
42,136 KB
testcase_13 AC 155 ms
40,156 KB
testcase_14 AC 155 ms
42,260 KB
testcase_15 AC 155 ms
42,148 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];
        var w = new int[3] { t[0], t[1], t[2] };
        dp[2] = check(w) ? t[0] : 0;
        for (int i = 3; i < n; i++)
        {
            if (check(new int[] { 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[] t)
    {
        if (!(t[0] != t[1] && t[1] != t[2] && t[0] != t[2])) return false;
        if (t[1] > t[0] && t[1] > t[2]) return true;
        if (t[1] < t[0] && t[1] < t[2]) return true;
        return false;
    }
}
0