結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー simansiman
提出日時 2021-08-26 07:16:53
言語 Ruby
(3.3.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 791 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 11,208 KB
実行使用メモリ 37,740 KB
最終ジャッジ日時 2023-08-11 05:29:23
合計ジャッジ時間 7,711 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,224 KB
testcase_01 AC 79 ms
15,068 KB
testcase_02 AC 234 ms
15,424 KB
testcase_03 AC 113 ms
15,256 KB
testcase_04 AC 145 ms
15,256 KB
testcase_05 AC 395 ms
37,608 KB
testcase_06 AC 387 ms
37,480 KB
testcase_07 AC 293 ms
37,740 KB
testcase_08 AC 395 ms
37,568 KB
testcase_09 AC 365 ms
37,032 KB
testcase_10 AC 365 ms
36,956 KB
testcase_11 AC 367 ms
36,896 KB
testcase_12 AC 365 ms
36,804 KB
testcase_13 AC 369 ms
36,904 KB
testcase_14 AC 372 ms
36,908 KB
testcase_15 AC 366 ms
36,864 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

T = gets.to_i

T.times do
  n = gets.to_i
  a = gets.split.map(&:to_i)
  dp = Array.new(n + 3) { Array.new(3, 0) }
  ans = 0

  n.times do |i|
    j = (i + 1) % n
    k = (i + 2) % n

    3.times do |d|
      if dp[i + 1][d] < dp[i][d]
        dp[i + 1][d] = dp[i][d]
      end
    end

    next if a[i] == a[j] || a[i] == a[k] || a[j] == a[k]
    next if a[i] < a[j] && a[j] < a[k]
    next if a[i] > a[j] && a[j] > a[k]

    if 0 <= i && i + 2 < n
      dp[i + 3][0] = dp[i][0] + a[i]
      ans = dp[i + 3][0] if ans < dp[i + 3][0]
    end

    if 1 <= i && i + 1 < n
      dp[i + 3][1] = dp[i][1] + a[i]
      ans = dp[i + 3][1] if ans < dp[i + 3][1]
    end

    if 2 <= i
      dp[i + 3][2] = dp[i][2] + a[i]
      ans = dp[i + 3][2] if ans < dp[i + 3][2]
    end
  end

  puts ans
end
0