結果

問題 No.305 鍵(2)
ユーザー shi-moshi-mo
提出日時 2016-10-21 19:29:05
言語 Ruby
(3.3.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 748 bytes
コンパイル時間 55 ms
コンパイル使用メモリ 11,484 KB
実行使用メモリ 31,408 KB
平均クエリ数 54.00
最終ジャッジ日時 2023-09-24 00:38:25
合計ジャッジ時間 2,299 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
30,832 KB
testcase_01 AC 102 ms
31,132 KB
testcase_02 AC 89 ms
30,900 KB
testcase_03 AC 91 ms
30,948 KB
testcase_04 AC 90 ms
31,408 KB
testcase_05 AC 88 ms
30,968 KB
testcase_06 AC 88 ms
31,044 KB
testcase_07 AC 89 ms
30,908 KB
testcase_08 AC 90 ms
31,012 KB
testcase_09 AC 89 ms
31,056 KB
testcase_10 AC 94 ms
30,872 KB
testcase_11 AC 91 ms
30,884 KB
testcase_12 AC 91 ms
31,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def query(n)
  puts n.join('')
  STDOUT.flush
end

def reply
  x, y = gets.split
  x = x.to_i
  exit 0 if 'unlocked' == y
  x
end

def detect_counts
  counts = Array.new(10)
  0.upto(9) do |i|
    query [i] * 10
    counts[i] = [i, reply]
  end
  counts
end

def detect_answer(counts)
  min_i, min_c = counts.sort_by{|i, c| c }.first
  numbers = counts.select{|i, c| 0 < c } - [[min_i, min_c]]
  answer = Array.new(10)

  numbers.each do |i, c|
    10.times do |j|
      q = Array.new(10){ min_i }
      q[j] = i

      query q
      n = reply
      next if n <= min_c

      answer[j] = i
    end
  end
  answer.map.with_index.select{|i, idx| i.nil? }.each do |i, idx|
    answer[idx] = min_i
  end
  query answer
end

detect_answer(detect_counts)
0