結果
| 問題 |
No.1804 Intersection of LIS
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-09 23:45:46 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 1,325 bytes |
| コンパイル時間 | 16,009 ms |
| コンパイル使用メモリ | 457,128 KB |
| 実行使用メモリ | 222,124 KB |
| 最終ジャッジ日時 | 2024-11-14 10:37:28 |
| 合計ジャッジ時間 | 43,234 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 TLE * 2 |
ソースコード
fun createLIS(permutation: List<Int>): List<Int> {
val minLast = IntArray(permutation.size + 1){permutation.size + 1}.also { it[0] = 0 }
val prevValue = IntArray(permutation.size + 1)
for (a in permutation) {
val pos = minLast.lowerBound(a)
prevValue[a] = minLast[pos - 1]
minLast[pos] = a
}
val result = mutableListOf<Int>((minLast.last { it != permutation.size + 1 }))
while (result.last() > 0) {
result.add(prevValue[result.last()])
}
result.removeLast()
return result.reversed()
}
inline fun IntArray.partitionPoint(predicate: (Int) -> Boolean): Int {
var min = 0
var max = size
while (min < max) {
val mid = (min + max) / 2
if (predicate(this[mid])) {
min = mid + 1
}else {
max = mid
}
}
return max
}
fun IntArray.lowerBound(target: Int): Int {
return partitionPoint { it < target }
}
fun main() {
val n = readLine()!!.trim().toInt()
val permutation = readLine()!!.trim().split(' ').map(String::toInt)
val forward = createLIS(permutation)
val backward = createLIS(permutation.reversed().map { n - it + 1 }).reversed().map { n - it + 1 }
val fix = forward.intersect(backward)
println(fix.size)
println(fix.toList().sorted().joinToString(" "))
}