結果

問題 No.1391 ±1 Abs Sum
ユーザー Kiri8128Kiri8128
提出日時 2021-02-12 22:07:08
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 977 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 265,020 KB
最終ジャッジ日時 2023-09-27 04:13:54
合計ジャッジ時間 24,654 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,720 KB
testcase_01 AC 68 ms
71,332 KB
testcase_02 AC 69 ms
71,280 KB
testcase_03 AC 69 ms
71,288 KB
testcase_04 AC 70 ms
71,336 KB
testcase_05 AC 69 ms
71,504 KB
testcase_06 AC 72 ms
71,156 KB
testcase_07 AC 71 ms
71,376 KB
testcase_08 AC 80 ms
76,008 KB
testcase_09 AC 80 ms
75,736 KB
testcase_10 AC 91 ms
76,120 KB
testcase_11 TLE -
testcase_12 AC 1,886 ms
260,568 KB
testcase_13 TLE -
testcase_14 AC 1,342 ms
259,512 KB
testcase_15 AC 1,795 ms
259,724 KB
testcase_16 AC 1,829 ms
260,036 KB
testcase_17 AC 1,527 ms
260,484 KB
testcase_18 TLE -
testcase_19 AC 1,568 ms
260,248 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def f(k):
    b = A[k]
    L = sorted([abs(a - b) for a in A])
    return sum(L[:K]) - sum(L[K:])

N, K = map(int, input().split())
A = [int(a) for a in input().split()]

# 1
l, r = 0, N - 1
fl, fr = f(l), f(r)
ans = min(fl, fr)
while r - l > 2:
    m1 = (l * 2 + r) // 3
    m2 = (l + r * 2) // 3
    fm1, fm2 = f(m1), f(m2)
    if fm1 < fm2:
        r = m2
        ans = min(ans, fm1)
    else:
        l = m1
        ans = min(ans, fm2)
if r - l > 1:
    ans = min(ans, f(l + r >> 1))

# 2
mi = 10 ** 60
mii = -1
s = max(1, N // 500)
for i in range(0, N, s):
    fi = f(i)
    if fi < mi:
        mi = fi
        mii = i
ans = min(ans, mi)
l = max(0, mii - s)
r = min(N, mii + s)
while r - l > 3:
    m1 = (l * 2 + r) // 3
    m2 = (l + r * 2) // 3
    fm1, fm2 = f(m1), f(m2)
    if fm1 < fm2:
        r = m2
        ans = min(ans, fm1)
    else:
        l = m1
        ans = min(ans, fm2)
if r - l > 1:
    for i in range(l + 1, r):
        ans = min(ans, f(i))
print(ans)
0