結果

問題 No.1830 Balanced Majority
ユーザー yudedakoyudedako
提出日時 2022-02-17 23:50:07
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 872 ms / 2,000 ms
コード長 3,428 bytes
コンパイル時間 10,779 ms
コンパイル使用メモリ 261,612 KB
実行使用メモリ 76,468 KB
平均クエリ数 11.69
最終ジャッジ日時 2023-09-11 17:57:08
合計ジャッジ時間 36,684 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 823 ms
74,728 KB
testcase_01 AC 807 ms
75,508 KB
testcase_02 AC 840 ms
75,612 KB
testcase_03 AC 841 ms
75,208 KB
testcase_04 AC 827 ms
75,768 KB
testcase_05 AC 809 ms
76,468 KB
testcase_06 AC 813 ms
75,328 KB
testcase_07 AC 816 ms
75,008 KB
testcase_08 AC 826 ms
75,224 KB
testcase_09 AC 821 ms
75,672 KB
testcase_10 AC 846 ms
76,068 KB
testcase_11 AC 845 ms
75,760 KB
testcase_12 AC 828 ms
75,004 KB
testcase_13 AC 827 ms
76,088 KB
testcase_14 AC 834 ms
74,824 KB
testcase_15 AC 830 ms
75,808 KB
testcase_16 AC 828 ms
76,060 KB
testcase_17 AC 831 ms
74,764 KB
testcase_18 AC 856 ms
75,436 KB
testcase_19 AC 829 ms
75,232 KB
testcase_20 AC 836 ms
75,304 KB
testcase_21 AC 872 ms
75,264 KB
testcase_22 AC 822 ms
75,644 KB
testcase_23 AC 824 ms
75,196 KB
testcase_24 AC 848 ms
76,256 KB
testcase_25 AC 834 ms
75,644 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