結果

問題 No.253 ロウソクの長さ
コンテスト
ユーザー くわい
提出日時 2015-10-24 13:50:10
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 1,111 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,995 ms
コンパイル使用メモリ 261,952 KB
実行使用メモリ 73,852 KB
平均クエリ数 28.69
最終ジャッジ日時 2026-03-09 14:07:07
合計ジャッジ時間 22,522 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import scala.annotation.tailrec
import scala.io.StdIn

object Problem253 {

  val threshold: Int = 50

  def question(len: Int): Int = {
    println("? " + len)
    StdIn.readInt()
  }

  def answer(len: Int): Unit = {
    println("! " + len)
  }

  def waitBurnout(): Int = (1 to threshold + 1).find(x => question(0) == 0).get

  def search(): Int = {
    @tailrec
    def searchR(min: Int, max: Int, burned: Int): Int = {
      val mid = (min + max) / 2

      question(mid) match {
        case 1 => searchR(mid - 1, max - 1, burned + 1)
        case 0 => mid + burned
        case -1 => searchR(min - 1, mid - 1, burned + 1)
      }
    }

    val min = threshold
    val max = Math.pow(10, 9).toInt
    searchR(min, max, 1)
  }

  def proc(): Int = {
    question(threshold) match {
      case 1 => search() // 二分探索
      case 0 => threshold // 一回で当たった
      case -1 => waitBurnout() // 0以下の処理が面倒なので長さが閾値以下の場合は燃え尽きるまで待つ
    }
  }

  def main(args: Array[String]): Unit = {
    val result = proc()
    answer(result)
  }
}
0