結果

問題 No.594 壊れた宝物発見機
ユーザー simansiman
提出日時 2022-08-04 13:16:23
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 1,139 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 11,280 KB
実行使用メモリ 31,836 KB
平均クエリ数 330.00
最終ジャッジ日時 2023-10-12 10:39:35
合計ジャッジ時間 6,487 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:5: warning: assigned but unused variable - d
Main.rb:53: warning: assigned but unused variable - z
Syntax OK

ソースコード

diff #

def send_query(x, y, z)
  STDOUT.puts("? %d %d %d" % [x, y, z])
  STDOUT.flush

  d = gets.to_i
end

lefts = [-200, -200, -200]
rights = [200, 200, 200]

def ternary_search_tree(left, right)
  loop_cnt = 55 

  loop_cnt.times do
    if yield((left * 2 + right) / 3) < yield((left + right * 2) / 3)
      right = (left + right * 2) / 3
    else
      left = (left * 2 + right) / 3
    end
  end

  (left + right) / 2
end

ans = [0, 0, 0]

3.times do |i|
  l = lefts[i]
  r = rights[i]

  if i == 0
    x = ternary_search_tree(l, r) { |x| send_query(x, ans[1], ans[2]) }
    ans[0] = x
  elsif i == 1
    y = ternary_search_tree(l, r) { |y| send_query(ans[0], y, ans[2]) }
    ans[1] = y
  else
    z = ternary_search_tree(l, r) { |z| send_query(ans[0], ans[1], z) }
    ans[2] = z
  end
end

min_dist = Float::INFINITY
best_ans = ans.dup

3.times do |i|
  x = ans[0] + (i - 1)

  3.times do |j|
    y = ans[1] + (j - 1)

    3.times do |k|
      z = ans[2] + (k - 1)
    end

    d = send_query(x, y, z)

    if min_dist > d
      min_dist = d
      best_ans = [x, y, z]
    end
  end
end

STDOUT.puts("! %d %d %d" % best_ans)
STDOUT.flush
0