結果

問題 No.45 回転寿司
ユーザー tshigitshigi
提出日時 2016-08-01 14:30:38
言語 Ruby
(3.3.0)
結果
AC  
実行時間 103 ms / 5,000 ms
コード長 1,175 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 11,560 KB
実行使用メモリ 15,972 KB
最終ジャッジ日時 2023-08-27 14:55:51
合計ジャッジ時間 4,483 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
15,532 KB
testcase_01 AC 94 ms
15,488 KB
testcase_02 AC 98 ms
15,648 KB
testcase_03 AC 100 ms
15,948 KB
testcase_04 AC 93 ms
15,532 KB
testcase_05 AC 84 ms
15,264 KB
testcase_06 AC 90 ms
15,700 KB
testcase_07 AC 100 ms
15,804 KB
testcase_08 AC 85 ms
15,428 KB
testcase_09 AC 101 ms
15,800 KB
testcase_10 AC 90 ms
15,244 KB
testcase_11 AC 87 ms
15,196 KB
testcase_12 AC 101 ms
15,704 KB
testcase_13 AC 83 ms
15,112 KB
testcase_14 AC 84 ms
15,264 KB
testcase_15 AC 90 ms
15,372 KB
testcase_16 AC 87 ms
15,316 KB
testcase_17 AC 89 ms
15,544 KB
testcase_18 AC 87 ms
15,508 KB
testcase_19 AC 95 ms
15,680 KB
testcase_20 AC 82 ms
15,092 KB
testcase_21 AC 83 ms
15,140 KB
testcase_22 AC 81 ms
15,088 KB
testcase_23 AC 81 ms
15,348 KB
testcase_24 AC 81 ms
15,040 KB
testcase_25 AC 81 ms
15,256 KB
testcase_26 AC 92 ms
15,452 KB
testcase_27 AC 97 ms
15,680 KB
testcase_28 AC 91 ms
15,484 KB
testcase_29 AC 95 ms
15,740 KB
testcase_30 AC 95 ms
15,592 KB
testcase_31 AC 80 ms
15,240 KB
testcase_32 AC 81 ms
15,084 KB
testcase_33 AC 103 ms
15,972 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

inputs = STDIN.readlines.map(&:chomp)
N = inputs[0].to_i
Vs = inputs[1].split(/\s+/).map(&:to_i)

class Block
  def initialize(range, head_throughs, tail_throughs, max_value)
    @range = range
    @head_throughs = head_throughs
    @tail_throughs = tail_throughs
    @max_value = max_value
  end

  attr_reader :range, :head_throughs, :tail_throughs, :max_value
end

def merge(blocks1, blocks2)
  blocks1.product(blocks2).select { |block1, block2|
    throughs = block1.tail_throughs + block2.head_throughs
    1 <= throughs && throughs <= 2
  }.map { |block1, block2|
    Block.new(
      block1.range.first..block2.range.last,
      block1.head_throughs,
      block2.tail_throughs,
      block1.max_value + block2.max_value
    )
  }.group_by { |block|
    [block.head_throughs, block.tail_throughs]
  }.map { |throughs, blocks|
    blocks.max_by(&:max_value)
  }
end

cands = Vs.map.with_index { |v, i|
  [Block.new(i..i, 0, 0, v), Block.new(i..i, 1, 1, 0)]
}

until cands.size == 1
  cands = cands.each_slice(2).map { |blocks1, blocks2|
    if blocks2
      merge(blocks1, blocks2)
    else
      blocks1
    end
  }
end

puts cands.first.max_by(&:max_value).max_value
0