結果
| 問題 |
No.305 鍵(2)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-21 19:45:48 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 108 ms / 2,000 ms |
| コード長 | 773 bytes |
| コンパイル時間 | 237 ms |
| コンパイル使用メモリ | 7,424 KB |
| 実行使用メモリ | 29,024 KB |
| 平均クエリ数 | 35.92 |
| 最終ジャッジ日時 | 2024-07-17 00:32:22 |
| 合計ジャッジ時間 | 2,531 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 |
コンパイルメッセージ
Syntax OK
ソースコード
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|
next if answer[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)