結果

問題 No.838 Noelちゃんと星々3
ユーザー rutilicus
提出日時 2020-11-03 10:07:45
言語 Kotlin
(2.1.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 769 bytes
コンパイル時間 8,917 ms
コンパイル使用メモリ 418,580 KB
最終ジャッジ日時 2025-03-14 06:11:17
合計ジャッジ時間 10,029 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Main.kt:13:22: error: 'fun StringBuilder.appendln(value: Long): StringBuilder' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
        2 -> builder.appendln(ys[1] - ys[0])
                     ^^^^^^^^
Main.kt:14:22: error: 'fun StringBuilder.appendln(value: Long): StringBuilder' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
        3 -> builder.appendln(ys[2] - ys[0])
                     ^^^^^^^^
Main.kt:23:21: error: 'fun StringBuilder.appendln(value: Long): StringBuilder' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
            builder.appendln(dp[n - 1])
                    ^^^^^^^^

ソースコード

diff #

import kotlin.math.min

fun main() {
    val builder = StringBuilder()

    // 解説読んだ
    val n = readInputLine().toInt()

    val ys = readInputLine().split(" ").map { it.toLong() }.toLongArray()
    ys.sort()

    when(n) {
        2 -> builder.appendln(ys[1] - ys[0])
        3 -> builder.appendln(ys[2] - ys[0])
        else -> {
            val dp = LongArray(n)
            dp[0] = 1_000_000_000_000L
            dp[1] = ys[1] - ys[0]
            dp[2] = ys[2] - ys[0]
            for (i in 3 until n) {
                dp[i] = min(dp[i - 2] + ys[i] - ys[i - 1], dp[i - 3] + ys[i] - ys[i - 2])
            }
            builder.appendln(dp[n - 1])
        }
    }

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}
0