結果

問題 No.992 最長増加部分列の数え上げ
ユーザー yakamotoyakamoto
提出日時 2020-02-14 22:46:56
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,496 ms / 2,000 ms
コード長 3,600 bytes
コンパイル時間 19,472 ms
コンパイル使用メモリ 466,904 KB
実行使用メモリ 132,324 KB
最終ジャッジ日時 2024-04-16 02:46:25
合計ジャッジ時間 67,225 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 349 ms
54,788 KB
testcase_01 AC 344 ms
54,480 KB
testcase_02 AC 343 ms
54,560 KB
testcase_03 AC 350 ms
54,368 KB
testcase_04 AC 838 ms
94,188 KB
testcase_05 AC 844 ms
93,640 KB
testcase_06 AC 949 ms
106,140 KB
testcase_07 AC 891 ms
94,220 KB
testcase_08 AC 743 ms
80,060 KB
testcase_09 AC 901 ms
93,820 KB
testcase_10 AC 927 ms
105,832 KB
testcase_11 AC 1,105 ms
109,892 KB
testcase_12 AC 670 ms
71,740 KB
testcase_13 AC 820 ms
93,632 KB
testcase_14 AC 872 ms
93,760 KB
testcase_15 AC 684 ms
72,860 KB
testcase_16 AC 1,276 ms
123,552 KB
testcase_17 AC 734 ms
79,792 KB
testcase_18 AC 824 ms
93,788 KB
testcase_19 AC 1,105 ms
109,956 KB
testcase_20 AC 1,368 ms
130,068 KB
testcase_21 AC 1,362 ms
130,228 KB
testcase_22 AC 1,325 ms
130,300 KB
testcase_23 AC 1,408 ms
130,104 KB
testcase_24 AC 1,335 ms
130,208 KB
testcase_25 AC 1,362 ms
130,176 KB
testcase_26 AC 1,496 ms
132,324 KB
testcase_27 AC 1,444 ms
130,320 KB
testcase_28 AC 1,379 ms
124,276 KB
testcase_29 AC 1,464 ms
132,136 KB
testcase_30 AC 1,084 ms
124,356 KB
testcase_31 AC 1,088 ms
124,560 KB
testcase_32 AC 1,082 ms
125,548 KB
testcase_33 AC 1,056 ms
125,420 KB
testcase_34 AC 1,060 ms
124,292 KB
testcase_35 AC 896 ms
116,192 KB
testcase_36 AC 912 ms
115,980 KB
testcase_37 AC 913 ms
116,204 KB
testcase_38 AC 921 ms
116,136 KB
testcase_39 AC 911 ms
116,256 KB
testcase_40 AC 1,094 ms
123,160 KB
testcase_41 AC 1,115 ms
123,292 KB
testcase_42 AC 1,141 ms
123,716 KB
testcase_43 AC 1,115 ms
123,292 KB
testcase_44 AC 1,085 ms
122,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val N = ni()
    val A = na(N)
    val (ix, pos) = LIS(A, N)

    debug{ix.joinToString { it.toString() }}
    debug(pos)

    val C = Array(ix.size){ BIT(ix[it].size) }
    for (i in 0 until N) {
      val p = pos[i]
      val cnt = if (p == 0) 1 else C[p-1].sum(ix[p-1][i]!!)
      debug{"$cnt"}
      C[p].add(ix[p][i]!!, cnt)
    }
    val ans = C[ix.lastIndex].sum(ix.last().size)
    out.println(ans)
  }

  fun LIS(A: IntArray, N: Int): Pair<Array<MutableMap<Int, Int>>, IntArray> {
    val ix = Array(N){ mutableListOf<Int>()}
    val lis = IntArray(N)
    var p = 0

    val pos = IntArray(N)
    for (i in 0 until A.size) {
      var l = -1
      var h = p
      while(h - l > 1) {
        val m = (h + l) / 2
        if (lis[m] >= A[i]) h = m
        else l = m
      }
      lis[h] = A[i]
      ix[h].add(i)
      if (h > 0) ix[h-1].add(i)
      p = max(p, h + 1)
      pos[i] = h
    }

    val ix2 = Array(p){ mutableMapOf<Int, Int>() }
    for (i in 0 until p) {
      ix[i].sortWith(compareBy({A[it]}, {-it}))
      for (j in 0 until ix[i].size) {
        ix2[i][ix[i][j]] = j
      }
    }
    return Pair(ix2, pos)
  }

  class BIT(n: Int) {
    private val N =
      if (Integer.highestOneBit(n) == n) n
      else Integer.highestOneBit(n) shl 1

    private val bit = LongArray(N + 1)

    fun sum(i: Int): Long {
      var x = i
      var s = 0L
      while(x > 0) {
        s += bit[x]
        x -= x and -x
      }
      return s % MOD
    }

    fun add(i: Int, a: Long) {
      var x = i + 1
      while(x <= N) {
        bit[x] = (bit[x] + a) % MOD
        x += x and -x
      }
    }
  }










































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

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n, offset) { ni() }
  }

  private inline fun map(n: Int, offset: Int = 0, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i + offset)
    }
    return res
  }

  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)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun <A, B> pair(a: A, b: B) = RPair(a, b)
data class RPair<A, B>(val _1: A, val _2: B)

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0