結果

問題 No.973 余興
ユーザー yakamotoyakamoto
提出日時 2020-01-18 15:20:12
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 3,353 ms / 4,000 ms
コード長 3,242 bytes
コンパイル時間 18,470 ms
コンパイル使用メモリ 438,636 KB
実行使用メモリ 129,844 KB
最終ジャッジ日時 2023-09-09 22:40:11
合計ジャッジ時間 126,085 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,476 ms
128,504 KB
testcase_01 AC 2,446 ms
128,560 KB
testcase_02 AC 2,451 ms
128,516 KB
testcase_03 AC 2,428 ms
128,492 KB
testcase_04 AC 2,471 ms
128,676 KB
testcase_05 AC 2,455 ms
128,600 KB
testcase_06 AC 2,439 ms
128,700 KB
testcase_07 AC 2,478 ms
128,544 KB
testcase_08 AC 2,789 ms
128,916 KB
testcase_09 AC 2,449 ms
128,512 KB
testcase_10 AC 2,305 ms
128,660 KB
testcase_11 AC 1,182 ms
74,500 KB
testcase_12 AC 1,151 ms
74,512 KB
testcase_13 AC 1,159 ms
74,432 KB
testcase_14 AC 1,168 ms
74,524 KB
testcase_15 AC 448 ms
56,144 KB
testcase_16 AC 433 ms
55,932 KB
testcase_17 AC 415 ms
55,964 KB
testcase_18 AC 425 ms
55,916 KB
testcase_19 AC 428 ms
56,544 KB
testcase_20 AC 420 ms
56,164 KB
testcase_21 AC 436 ms
55,920 KB
testcase_22 AC 433 ms
55,848 KB
testcase_23 AC 428 ms
56,180 KB
testcase_24 AC 430 ms
56,552 KB
testcase_25 AC 400 ms
55,452 KB
testcase_26 AC 413 ms
56,036 KB
testcase_27 AC 433 ms
55,912 KB
testcase_28 AC 428 ms
55,904 KB
testcase_29 AC 428 ms
55,828 KB
testcase_30 AC 3,020 ms
129,500 KB
testcase_31 AC 2,998 ms
129,384 KB
testcase_32 AC 2,980 ms
129,388 KB
testcase_33 AC 3,011 ms
129,504 KB
testcase_34 AC 3,269 ms
129,416 KB
testcase_35 AC 3,353 ms
129,844 KB
testcase_36 AC 3,295 ms
129,472 KB
testcase_37 AC 2,967 ms
129,392 KB
testcase_38 AC 3,069 ms
129,432 KB
testcase_39 AC 3,069 ms
129,436 KB
testcase_40 AC 303 ms
52,852 KB
testcase_41 AC 306 ms
52,964 KB
testcase_42 AC 300 ms
52,840 KB
testcase_43 AC 301 ms
52,656 KB
testcase_44 AC 303 ms
52,760 KB
testcase_45 AC 317 ms
52,976 KB
testcase_46 AC 2,966 ms
129,428 KB
testcase_47 AC 3,002 ms
129,448 KB
testcase_48 AC 3,001 ms
129,416 KB
testcase_49 AC 2,724 ms
129,456 KB
testcase_50 AC 2,987 ms
129,612 KB
testcase_51 AC 2,975 ms
129,748 KB
testcase_52 AC 3,097 ms
129,524 KB
testcase_53 AC 3,004 ms
129,672 KB
testcase_54 AC 3,057 ms
129,396 KB
testcase_55 AC 3,060 ms
129,596 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:82:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun f(a: Boolean, b: Boolean) = a and b
          ^

ソースコード

diff #

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

val MOD = 1_000_000_007

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

  val dpL = Array(N){SegmentTree(N - it, true)}
  val dpR = Array(N){SegmentTree(it + 1, true)}
  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 + 1) {
      val r = l + len - 1
      val win = !dpL[l].query(max(0, len-mxL[r]-1), len-1) or
              !dpR[r].query(max(0, len-mxR[l]-1), len-1)
      debug{"$l $r $win"}
      dpL[l].update(len - 1, win)
      dpR[r].update(len - 1, win)
    }
  }

  if(isDebug) {
    for (l in 0 until N) {
      val flags = mutableListOf<Boolean>()
      for (r in l until N) {
        val len = r-l
        assert(dpL[l].query(len, len+1) == dpR[r].query(len, len+1))
        flags.add(dpR[r].query(len, len+1))
      }
      debug(flags.toBooleanArray())
    }
  }

  if (dpL[0].query(N-1, N)) println("A")
  else println("B")
}

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

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

  private inline fun f(a: Boolean, b: Boolean) = a and b

  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