結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2021-02-12 22:55:07
言語 Kotlin
(1.9.23)
結果
TLE  
実行時間 -
コード長 734 bytes
コンパイル時間 13,054 ms
コンパイル使用メモリ 428,016 KB
実行使用メモリ 88,820 KB
最終ジャッジ日時 2023-09-27 05:57:43
合計ジャッジ時間 68,039 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 288 ms
57,336 KB
testcase_01 AC 287 ms
56,980 KB
testcase_02 AC 294 ms
57,044 KB
testcase_03 AC 284 ms
57,048 KB
testcase_04 AC 292 ms
57,032 KB
testcase_05 AC 292 ms
56,972 KB
testcase_06 AC 291 ms
56,924 KB
testcase_07 AC 291 ms
57,036 KB
testcase_08 AC 300 ms
57,076 KB
testcase_09 AC 300 ms
57,440 KB
testcase_10 AC 299 ms
57,056 KB
testcase_11 TLE -
testcase_12 AC 1,971 ms
81,800 KB
testcase_13 TLE -
testcase_14 AC 1,151 ms
81,596 KB
testcase_15 AC 1,270 ms
81,632 KB
testcase_16 AC 1,613 ms
82,656 KB
testcase_17 AC 1,461 ms
81,852 KB
testcase_18 AC 1,662 ms
83,296 KB
testcase_19 AC 1,810 ms
82,132 KB
testcase_20 WA -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 1,938 ms
81,824 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 1,926 ms
83,556 KB
testcase_32 AC 1,172 ms
79,940 KB
testcase_33 AC 1,182 ms
79,992 KB
testcase_34 TLE -
testcase_35 AC 280 ms
57,048 KB
testcase_36 AC 1,287 ms
80,840 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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