結果

問題 No.850 企業コンテスト2位
ユーザー yakamotoyakamoto
提出日時 2019-07-12 03:00:24
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 876 ms / 2,000 ms
コード長 5,725 bytes
コンパイル時間 19,386 ms
コンパイル使用メモリ 260,404 KB
実行使用メモリ 77,472 KB
平均クエリ数 200.96
最終ジャッジ日時 2023-09-23 17:54:19
合計ジャッジ時間 36,145 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 820 ms
76,692 KB
testcase_01 AC 802 ms
76,552 KB
testcase_02 AC 816 ms
76,212 KB
testcase_03 AC 787 ms
76,536 KB
testcase_04 AC 793 ms
76,128 KB
testcase_05 AC 798 ms
76,204 KB
testcase_06 AC 802 ms
76,512 KB
testcase_07 AC 826 ms
76,128 KB
testcase_08 AC 841 ms
77,024 KB
testcase_09 AC 839 ms
76,744 KB
testcase_10 AC 865 ms
76,336 KB
testcase_11 AC 869 ms
76,444 KB
testcase_12 AC 854 ms
76,396 KB
testcase_13 AC 871 ms
76,424 KB
testcase_14 AC 852 ms
77,108 KB
testcase_15 AC 860 ms
76,892 KB
testcase_16 AC 846 ms
76,788 KB
testcase_17 AC 855 ms
76,728 KB
testcase_18 AC 868 ms
77,472 KB
testcase_19 AC 868 ms
76,932 KB
testcase_20 AC 873 ms
76,680 KB
testcase_21 AC 861 ms
76,532 KB
testcase_22 AC 875 ms
76,804 KB
testcase_23 AC 866 ms
76,364 KB
testcase_24 AC 876 ms
76,916 KB
testcase_25 AC 866 ms
76,692 KB
testcase_26 AC 858 ms
76,312 KB
testcase_27 AC 789 ms
76,880 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 + used))
      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