結果

問題 No.1826 Fruits Collecting
ユーザー yudedako
提出日時 2022-01-31 16:16:38
言語 Kotlin
(2.1.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,490 bytes
コンパイル時間 15,397 ms
コンパイル使用メモリ 455,076 KB
実行使用メモリ 117,684 KB
最終ジャッジ日時 2024-06-11 08:57:47
合計ジャッジ時間 74,667 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33 TLE * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

import kotlin.math.absoluteValue

class MaxBit(val size: Int) {
    private val array = LongArray(size){0}
    fun get(position: Int): Long {
        var result = 0L
        var pos = position
        while (pos in array.indices) {
            result = maxOf(result, array[pos])
            pos -= pos.inv() and (pos + 1)
        }
        return result
    }
    fun add(position: Int, value: Long) {
        var pos = position
        while (pos in array.indices) {
            array[pos] = maxOf(array[pos], value)
            pos += pos.inv() and (pos + 1)
        }
    }
    fun getAll(): Long = get(size - 1)
}
fun List<Int>.lowerBound(value: Int): Int {
    var min = 0
    var max = size
    while (min < max) {
        val mid = (min + max) ushr 1
        if (this[mid] < value) {
            min = mid + 1
        }else {
            max = mid
        }
    }
    return max
}
fun main() {
    val n = readLine()!!.toInt()
    val fruits = List(n){
        val (t, x, v) = readLine()!!.split(' ').map(String::toInt)
        Triple(t, x, v)
    }
    val diff = fruits.map { (t, x, _) -> t - x}.distinct().sorted()
    val bit = MaxBit(diff.size)
    for ((t, x, v) in fruits.sortedWith(compareBy<Triple<Int, Int, Int>> { (t, x, _) -> t + x}.thenBy { (t, _, _) -> t })) {
        if (t < x.absoluteValue) continue
        val di = diff.lowerBound(t - x)
        val maxValue = bit.get(di) + v
        bit.add(di, maxValue)
    }
    val result = bit.getAll()
    println(result)
}
0