結果
問題 | No.3 ビットすごろく |
ユーザー |
|
提出日時 | 2020-05-22 12:01:31 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 111 ms / 5,000 ms |
コード長 | 797 bytes |
コンパイル時間 | 48 ms |
コンパイル使用メモリ | 7,680 KB |
実行使用メモリ | 13,568 KB |
最終ジャッジ日時 | 2024-07-01 09:48:12 |
合計ジャッジ時間 | 4,350 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
コンパイルメッセージ
Syntax OK
ソースコード
# frozen_string_literal: true # rubocop:todo Metrics/AbcSize # rubocop:todo Metrics/CyclomaticComplexity def solve # rubocop:todo Metrics/MethodLength return 1 if N == 1 positions = [0] squares = Array.new(N, nil) squares[0] = 0 (1..N).each do |step| positions = positions.map do |position| bit_count = BIT_COUNTS[position + 1] return step + 1 if (position + bit_count) == N - 1 [position - bit_count, position + bit_count].map do |i| if i >= 0 && i <= N - 1 && (squares[i].nil? || step < squares[i]) squares[i] = step i end end.compact end.flatten end -1 end # rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/AbcSize N = gets.to_i BIT_COUNTS = (0..N).map { _1.digits(2).sum } puts solve