結果

問題 No.594 壊れた宝物発見機
ユーザー simansiman
提出日時 2022-08-04 13:28:55
言語 Ruby
(3.3.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,184 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 11,164 KB
実行使用メモリ 31,516 KB
平均クエリ数 178.00
最終ジャッジ日時 2023-10-12 10:54:43
合計ジャッジ時間 5,798 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 200 ms
31,516 KB
testcase_01 AC 201 ms
31,256 KB
testcase_02 AC 198 ms
31,096 KB
testcase_03 AC 199 ms
30,772 KB
testcase_04 AC 197 ms
31,312 KB
testcase_05 AC 201 ms
30,724 KB
testcase_06 AC 201 ms
31,048 KB
testcase_07 AC 197 ms
30,848 KB
testcase_08 AC 196 ms
31,216 KB
testcase_09 AC 199 ms
31,232 KB
testcase_10 AC 199 ms
30,904 KB
testcase_11 AC 199 ms
30,960 KB
testcase_12 AC 197 ms
31,184 KB
testcase_13 AC 199 ms
30,800 KB
testcase_14 AC 199 ms
31,060 KB
testcase_15 AC 196 ms
31,236 KB
testcase_16 AC 197 ms
31,460 KB
testcase_17 AC 197 ms
31,172 KB
testcase_18 AC 199 ms
31,324 KB
testcase_19 AC 198 ms
31,008 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:5: warning: assigned but unused variable - d
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 = [-150, -150, -150]
rights = [150, 150, 150]

def ternary_search_tree(left, right)
  loop_cnt = 25

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

  ((left + right) / 2.0).round
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.round, ans[1], ans[2]) }
    ans[0] = x
  elsif i == 1
    y = ternary_search_tree(l, r) { |y| send_query(ans[0], y.round, ans[2]) }
    ans[1] = y
  else
    z = ternary_search_tree(l, r) { |z| send_query(ans[0], ans[1], z.round) }
    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)

      d = send_query(x, y, z)

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

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