結果

問題 No.973 余興
ユーザー yakamotoyakamoto
提出日時 2020-01-17 23:04:37
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 2,947 bytes
コンパイル時間 18,370 ms
コンパイル使用メモリ 436,448 KB
実行使用メモリ 237,308 KB
最終ジャッジ日時 2023-09-08 06:46:34
合計ジャッジ時間 89,451 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3,109 ms
235,528 KB
testcase_01 AC 3,072 ms
235,628 KB
testcase_02 AC 3,246 ms
235,548 KB
testcase_03 AC 3,079 ms
235,528 KB
testcase_04 AC 3,324 ms
235,528 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 3,058 ms
235,652 KB
testcase_11 AC 1,362 ms
112,768 KB
testcase_12 AC 1,398 ms
112,512 KB
testcase_13 WA -
testcase_14 AC 1,392 ms
112,588 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 406 ms
56,616 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 433 ms
56,372 KB
testcase_23 AC 414 ms
56,376 KB
testcase_24 AC 418 ms
56,476 KB
testcase_25 AC 388 ms
56,296 KB
testcase_26 AC 404 ms
56,228 KB
testcase_27 AC 451 ms
56,324 KB
testcase_28 WA -
testcase_29 AC 445 ms
56,348 KB
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 WA -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 AC 305 ms
52,960 KB
testcase_41 AC 308 ms
53,052 KB
testcase_42 AC 306 ms
54,788 KB
testcase_43 WA -
testcase_44 AC 305 ms
53,104 KB
testcase_45 WA -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
testcase_49 TLE -
testcase_50 TLE -
testcase_51 TLE -
testcase_52 TLE -
testcase_53 TLE -
testcase_54 TLE -
testcase_55 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007

fun main() {
  val (N, X) = readInts()
  val A = readInts()

  if (N == 1) {
    println("B")
    return
  }

  val dpL = Array(N){SegmentTree(N, true){a, b -> a and b}}
  val dpR = Array(N){SegmentTree(N, true){a, b -> a and b}}
  for (i in 0 until N) {
    // 1個だけ残ってる場合はlose
    dpL[i].update(0, false)
    dpR[i].update(0, false)
  }
  val mxR = run {
    val res = IntArray(N)
    var sum = 0L
    var r = N - 1
    for (l in N - 1 downTo 0) {
      sum += A[l]
      while (r > l && sum > X) {
        sum -= A[r]
        r--
      }
      res[l] = r - l + 1
    }
    res
  }
  debug(mxR)
  val mxL = run {
    val res = IntArray(N)
    var sum = 0L
    var l = 0
    for (r in 0 until N) {
      sum += A[r]
      while (l < r && sum > X) {
        sum -= A[l]
        l++
      }
      res[r] = r - l + 1
    }
    res
  }
  debug(mxL)

  for (len in 2 until N + 1) {
    for (l in 0 until N - len) {
      val r = l + len - 1
      val win = !dpL[l].query(0, min(len-1, mxR[l])) or
              !dpR[r].query(0, min(len-1, mxL[r]))
      dpL[l].update(len - 1, win)
      dpR[r].update(len - 1, win)
    }
  }
  if (dpL[0].query(N-1, N)) println("A")
  else println("B")
}

class SegmentTree(n: Int, val zero: Boolean, val f: (Boolean, Boolean) -> Boolean) {
  private val N =
    if (Integer.highestOneBit(n) == n) n
    else Integer.highestOneBit(n) shl 1

  private val dat = BooleanArray(2 * N){zero}

  fun update(i: Int, a: Boolean) {
    var ix = i + N
    dat[ix] = a
    while(ix > 1) {
      dat[ix shr 1] = f(dat[ix], dat[ix xor 1])
      ix = ix shr 1
    }
  }

  /**
   * [a, b)
   */
  fun query(a: Int, b: Int): Boolean {
    var res: Boolean = zero
    var left = a + N
    var right = b - 1 + N

    while(left <= right) {
      if ((left and 1) == 1) res = f(res, dat[left])
      if ((right and 1) == 0) res = f(res, dat[right])
      left = (left + 1) shr 1 // 右の子供なら右の親に移動
      right = (right - 1) shr 1 // 左の子供なら左の親に移動
    }

    return res
  }
}












































private val isDebug = try {
  // なんか本番でエラーでる
  System.getenv("MY_DEBUG") != null
} catch (t: Throwable) {
  false
}

private fun readLn() = readLine()!!
private fun readStrings() = readLn().split(" ")
private fun readInts() = readStrings().map { it.toInt() }.toIntArray()
private fun readLongs() = readStrings().map { it.toLong() }.toLongArray()
private inline fun debug(msg: () -> String) {
  if (isDebug) System.err.println(msg())
}
private fun debug(a: LongArray) {
  debug{a.joinToString(" ")}
}
private fun debug(a: IntArray) {
  debug{a.joinToString(" ")}
}
private fun debug(a: BooleanArray) {
  debug{a.map{if(it) 1 else 0}.joinToString("")}
}
private fun debugDim(A: Array<IntArray>) {
  if (isDebug) {
    for (a in A) {
      debug(a)
    }
  }
}
0