結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー simansiman
提出日時 2021-08-26 07:16:53
言語 Ruby
(3.3.0)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 791 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 32,000 KB
最終ジャッジ日時 2024-04-28 22:18:33
合計ジャッジ時間 6,953 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
12,032 KB
testcase_01 AC 88 ms
12,160 KB
testcase_02 AC 273 ms
12,288 KB
testcase_03 AC 127 ms
12,288 KB
testcase_04 AC 160 ms
12,288 KB
testcase_05 AC 439 ms
32,000 KB
testcase_06 AC 432 ms
32,000 KB
testcase_07 AC 337 ms
31,872 KB
testcase_08 AC 438 ms
32,000 KB
testcase_09 AC 415 ms
30,848 KB
testcase_10 AC 415 ms
30,464 KB
testcase_11 AC 418 ms
30,848 KB
testcase_12 AC 411 ms
30,720 KB
testcase_13 AC 411 ms
30,720 KB
testcase_14 AC 415 ms
30,720 KB
testcase_15 AC 413 ms
30,720 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