結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2021-02-12 22:55:08
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 734 bytes
コンパイル時間 12,995 ms
コンパイル使用メモリ 427,560 KB
実行使用メモリ 86,864 KB
最終ジャッジ日時 2023-09-27 05:58:48
合計ジャッジ時間 51,141 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
56,872 KB
testcase_01 AC 297 ms
57,000 KB
testcase_02 AC 305 ms
57,040 KB
testcase_03 AC 302 ms
56,836 KB
testcase_04 AC 309 ms
56,796 KB
testcase_05 AC 304 ms
56,924 KB
testcase_06 AC 309 ms
56,984 KB
testcase_07 AC 310 ms
56,868 KB
testcase_08 AC 315 ms
56,980 KB
testcase_09 AC 316 ms
56,996 KB
testcase_10 AC 325 ms
57,124 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,389 ms
81,408 KB
testcase_15 AC 1,409 ms
81,340 KB
testcase_16 AC 1,912 ms
82,728 KB
testcase_17 AC 1,565 ms
83,016 KB
testcase_18 AC 1,845 ms
83,876 KB
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:9:9: warning: variable 'amin' is never used
    val amin = a.min()!!
        ^
Main.kt:9:23: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val amin = a.min()!!
                      ^
Main.kt:10:9: warning: variable 'amax' is never used
    val amax = a.max()!!
        ^
Main.kt:10:23: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val amax = a.max()!!
                      ^

ソースコード

diff #

import kotlin.collections.*
import kotlin.math.*

@kotlin.ExperimentalStdlibApi
fun main() {
    val (n, k) = readLine()!!.split(" ").map { it.toInt() }
    val a = readLine()!!.split(" ").map { it.toLong() }.toTypedArray().sorted()

    val amin = a.min()!!
    val amax = a.max()!!

    fun f(ai : Long) : Long {
        val l = a.map { abs(ai - it) }.sorted()
        return l.take(k).sum() - l.takeLast(n-k).sum()
    }

    var l = 0; var r = n - 1
    while(l + 10 < r) {
        val ll = (l+l+r) / 3
        val rr = (l+r+r) / 3

        if (f(a[ll]) < f(a[rr])) r = rr
        else l = ll
    }

    var ans = minOf(f(a.first()), f(a.last()))
    for (i in l .. r) {
        ans = minOf(ans, f(a[i]))
    }
    println(ans)
}
0