結果

問題 No.1830 Balanced Majority
ユーザー yudedakoyudedako
提出日時 2022-02-17 23:50:07
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 736 ms / 2,000 ms
コード長 3,428 bytes
コンパイル時間 13,151 ms
コンパイル使用メモリ 270,296 KB
実行使用メモリ 78,384 KB
平均クエリ数 11.69
最終ジャッジ日時 2024-06-29 07:54:30
合計ジャッジ時間 30,875 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 719 ms
77,456 KB
testcase_01 AC 698 ms
78,060 KB
testcase_02 AC 707 ms
77,900 KB
testcase_03 AC 707 ms
77,872 KB
testcase_04 AC 694 ms
77,620 KB
testcase_05 AC 697 ms
77,216 KB
testcase_06 AC 710 ms
77,284 KB
testcase_07 AC 716 ms
77,816 KB
testcase_08 AC 686 ms
76,820 KB
testcase_09 AC 689 ms
77,780 KB
testcase_10 AC 700 ms
77,024 KB
testcase_11 AC 706 ms
78,384 KB
testcase_12 AC 715 ms
77,844 KB
testcase_13 AC 723 ms
77,412 KB
testcase_14 AC 736 ms
78,352 KB
testcase_15 AC 720 ms
77,628 KB
testcase_16 AC 699 ms
77,360 KB
testcase_17 AC 709 ms
77,492 KB
testcase_18 AC 696 ms
77,808 KB
testcase_19 AC 707 ms
77,556 KB
testcase_20 AC 724 ms
77,280 KB
testcase_21 AC 681 ms
77,968 KB
testcase_22 AC 708 ms
77,440 KB
testcase_23 AC 718 ms
78,096 KB
testcase_24 AC 718 ms
77,552 KB
testcase_25 AC 709 ms
77,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.util._
trait Judge:
  val size: Int
  def askDiffLeft(until: Int): Int
  def askDiffRight(from: Int): Int
  def answer(from: Int, until: Int): Unit

class RandomJudge(override val size: Int, generator: Random) extends Judge:
  val state = Array.tabulate(size){_ & 1}.pipe(generator.shuffle)
  val sum = state.clone().tap{array =>
    for i <- 1 until size do
      array(i) += array(i - 1)
  }
  private var count: Int = 0
  def currentCount = count
  override def askDiffLeft(until: Int): Int =
    if until == 0 then
      0
    else
      count += 1
      val one = sum(until - 1)
      val zero = until - one
      one - zero

  override def askDiffRight(from: Int): Int =
    if from == size then
      0
    else if from == 0 then
      size / 2
    else
      count += 1
      val one = size / 2 - sum(from - 1)
      val zero = size - from - one
      one - zero

  override def answer(from: Int, until: Int) =
    if from > 0 then
      require((sum(until - 1) - sum(from - 1)) * 2 == until - from)
    else
      require(sum(until - 1) * 2 == (until - from))
    require(count <= 20)
    require((size / 2 until size).contains(until - from))

class SystemJudge(override val size: Int) extends Judge:
  private def ask(to: Int): Int =
    if to < 0 then
      0
    else if to + 1 == size then
      size / 2
    else
      println(s"? ${to + 1}")
      readLine.toInt
  override def askDiffLeft(until: Int): Int =
    val one = ask(until - 1)
    val zero = until - one
    one - zero

  override def askDiffRight(from: Int): Int =
    val one = size / 2 - ask(from - 1)
    val zero = size - from - one
    one - zero

  override def answer(from: Int, until: Int) =
    println(s"! ${from + 1} $until")

def test(size: Int, seed: Int): Int =
  val random = Random(seed)
  val judge = RandomJudge(size, random)
  solve(size, judge)
  judge.currentCount

def solve(size: Int, judge: Judge) =
  val left = size / 4
  val right = left + size / 2
  val leftDiff = judge.askDiffLeft(left)
  val rightDiff = judge.askDiffRight(right)
  if leftDiff == 0 then
    judge.answer(left, size)
  else if rightDiff == 0 then
    judge.answer(0, right)
  else if leftDiff + rightDiff == 0then
    judge.answer(left, right)
  else if leftDiff.sign * rightDiff.sign > 0 then
    var min = left
    var max = right
    while min < max do
      val mid = (min + max) >> 1
      val diff = judge.askDiffLeft(mid)
      if diff.sign == leftDiff.sign then
        min = mid + 1
      else
        max = mid
    if max >= size / 2 then
      judge.answer(0, max)
    else
      judge.answer(max, size)
  else if leftDiff.abs < rightDiff.abs then
    var min = right
    var max = size
    while min < max do
      val mid = (min + max) >> 1
      val diff = judge.askDiffRight(mid)
      if (diff + leftDiff).sign * rightDiff.sign > 0 then
        min = mid + 1
      else
        max = mid
    judge.answer(left, max)
  else
    var min = 0
    var max = left
    while min < max do
      val mid = (min + max) >> 1
      val diff = judge.askDiffLeft(mid)
      if (diff + rightDiff).sign * leftDiff.sign >= 0 then
        max = mid
      else
        min = mid + 1
    judge.answer(max, right)





@main def main =
  val size = readLine.toInt
  solve(size, SystemJudge(size))
0