結果

問題 No.2627 Unnatural Pitch
ユーザー AngrySadEightAngrySadEight
提出日時 2024-02-04 13:22:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 406 ms / 4,000 ms
コード長 1,155 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 260,740 KB
最終ジャッジ日時 2024-02-04 13:22:28
合計ジャッジ時間 7,080 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,600 KB
testcase_01 AC 38 ms
55,600 KB
testcase_02 AC 163 ms
202,300 KB
testcase_03 AC 38 ms
55,600 KB
testcase_04 AC 406 ms
260,740 KB
testcase_05 AC 331 ms
260,732 KB
testcase_06 AC 248 ms
192,820 KB
testcase_07 AC 148 ms
100,052 KB
testcase_08 AC 202 ms
129,800 KB
testcase_09 AC 292 ms
240,812 KB
testcase_10 AC 304 ms
240,684 KB
testcase_11 AC 67 ms
73,296 KB
testcase_12 AC 79 ms
76,752 KB
testcase_13 AC 41 ms
59,440 KB
testcase_14 AC 65 ms
73,304 KB
testcase_15 AC 65 ms
71,208 KB
testcase_16 AC 283 ms
163,048 KB
testcase_17 AC 354 ms
256,080 KB
testcase_18 AC 293 ms
142,564 KB
testcase_19 AC 304 ms
195,676 KB
testcase_20 AC 155 ms
100,548 KB
testcase_21 AC 236 ms
193,212 KB
testcase_22 AC 313 ms
215,916 KB
testcase_23 AC 235 ms
181,092 KB
testcase_24 AC 200 ms
112,532 KB
testcase_25 AC 278 ms
180,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

N, K, L, U = map(int, input().split())
A = list(map(int, input().split()))

diffs = U - L
ok = 0
ng = 1000000000000002

while ng - ok > 1:
    mid = (ok + ng) // 2
    lower = 0
    upper = 0
    for i in range(N):
        if A[i] < mid:
            lower += 1
        elif A[i] > mid + diffs:
            upper += 1
    if lower <= upper:
        ok = mid
    else:
        ng = mid

d = defaultdict(int)
for i in range(N):
    d[A[i]] += 1

ans = 10 ** 18 + 2

now_score = 0
now_L = ok - K
now_U = now_L + diffs

left_rem = [0 for _ in range(K)]
right_rem = [0 for _ in range(K)]

for i in range(N):
    if A[i] < now_L:
        now_score += (now_L - A[i] + K - 1) // K
        left_rem[A[i] % K] += 1
    if A[i] > now_U:
        now_score += (A[i] - now_U + K - 1) // K
        right_rem[A[i] % K] += 1

ans = min(ans, now_score)

for i in range(2 * K):
    left_rem[(now_L % K + K) % K] += d[now_L]
    now_score += left_rem[(now_L % K + K) % K]
    now_L += 1
    now_U += 1
    now_score -= right_rem[(now_U % K + K) % K]
    right_rem[(now_U % K + K) % K] -= d[now_U]
    ans = min(ans, now_score)

print(ans)
0