結果

問題 No.850 企業コンテスト2位
ユーザー yakamotoyakamoto
提出日時 2019-07-12 02:53:54
言語 Scala(Beta)
(3.4.0)
結果
RE  
実行時間 -
コード長 5,718 bytes
コンパイル時間 15,776 ms
コンパイル使用メモリ 274,864 KB
実行使用メモリ 81,528 KB
平均クエリ数 200.39
最終ジャッジ日時 2024-07-16 17:46:30
合計ジャッジ時間 48,691 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 843 ms
80,024 KB
testcase_02 AC 862 ms
80,424 KB
testcase_03 AC 850 ms
80,416 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 864 ms
80,156 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 RE -
testcase_22 RE -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 868 ms
80,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer

  import scala.collection.mutable.ArrayBuffer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[ArrayBuffer[Int]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i).mkString(" "))
    }
  }
  def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  type G = (Int, Seq[Int])
  def merge(g1: G, g2: G): G = {
    val res = queryMax(g1._1, g2._1)
    if (res == g1._1) {
      (g1._1, g1._2 ++ Seq(g2._1))
    } else {
      (g2._1, g2._2 ++ Seq(g1._1))
    }
  }

  /**
    * asのサイズは必ず2の乗数
    * @return (a, [b]) { a > b }
    */
  def makeGroup(as: Array[Int]): G = {
    if (as.length == 1) (as.head, Nil)
    else {
      val (bs, cs) = as.splitAt(as.length/2)
      val gb = makeGroup(bs)
      val gc = makeGroup(cs)
      merge(gb, gc)
    }
  }

  def queryMax(a: Int, b: Int): Int = {
    out.println(s"? $a $b")
    out.flush()
    ni()
  }

  def queryMax(as: Seq[Int]): Int = {
    as.reduce(queryMax)
  }

  def solve(): Unit = {
    val N = ni()

    var g: G = null
    var used = 0
    while(used < N) {
      val k = log2(N - used)
      val n = 1 << k
      debug(s"used:$used k:$k n:$n")
      val g1 = makeGroup(Array.tabulate(n)(_ + 1))
      if (g == null) g = g1
      else g = merge(g, g1)
      used += n
    }

    out.println(s"! ${queryMax(g._2)}")
    out.flush()
  }

  /**
    * log2して小数点切り捨てたもの
    */
  def log2(x: Int): Int = {
    assert(x > 0)
    var a = x
    var i = 0 // 何回2で割ったら1になるか
    while(a > 1) {
      i += 1
      a >>>= 1
    }
    i
  }

  def topologicalSort(g: Array[ArrayBuffer[Int]]): Array[Int] = {
    val n = g.length
    val L = Array.ofDim[Int](n)
    var ptr = 0
    val S = Array.ofDim[Int](n)
    var last, cur = 0
    val deg = Array.ofDim[Int](n)
    REP(n) { i =>
      REP(g(i).length) { j =>
        deg(g(i)(j)) += 1
      }
    }

    REP(n) { i =>
      if (deg(i) == 0) {
        S(last) = i
        last += 1
      }
    }

    while(cur < last) {
      val v = S(cur)
      cur += 1
      L(ptr) = v
      ptr += 1

      REP(g(v).length) { i =>
        val u = g(v)(i)
        deg(u) -= 1
        if (deg(u) == 0) {
          S(last) = u
          last += 1
        }
      }
    }

    // 閉路チェック
    REP(n) { i =>
      if (deg(i) > 0) return null
    }
    L
  }
}
0