結果

問題 No.2627 Unnatural Pitch
ユーザー AngrySadEightAngrySadEight
提出日時 2024-02-08 13:38:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 382 ms / 4,000 ms
コード長 1,326 bytes
コンパイル時間 380 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 261,236 KB
最終ジャッジ日時 2024-02-08 13:39:01
合計ジャッジ時間 7,326 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,600 KB
testcase_01 AC 40 ms
55,600 KB
testcase_02 AC 181 ms
202,300 KB
testcase_03 AC 39 ms
55,600 KB
testcase_04 AC 345 ms
261,232 KB
testcase_05 AC 350 ms
261,236 KB
testcase_06 AC 245 ms
192,880 KB
testcase_07 AC 162 ms
100,180 KB
testcase_08 AC 187 ms
129,700 KB
testcase_09 AC 308 ms
240,792 KB
testcase_10 AC 320 ms
240,660 KB
testcase_11 AC 74 ms
73,288 KB
testcase_12 AC 89 ms
76,780 KB
testcase_13 AC 42 ms
59,440 KB
testcase_14 AC 74 ms
73,324 KB
testcase_15 AC 68 ms
73,332 KB
testcase_16 AC 260 ms
163,040 KB
testcase_17 AC 382 ms
256,020 KB
testcase_18 AC 286 ms
142,552 KB
testcase_19 AC 326 ms
195,660 KB
testcase_20 AC 150 ms
100,568 KB
testcase_21 AC 242 ms
193,132 KB
testcase_22 AC 319 ms
215,824 KB
testcase_23 AC 227 ms
181,024 KB
testcase_24 AC 205 ms
112,608 KB
testcase_25 AC 262 ms
180,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

assert 1 <= N <= 200000
assert 1 <= K <= 200000
assert 0 <= L <= 10 ** 12
assert 0 <= U <= 10 ** 12
assert U - L >= K
for i in range(N):
    assert 0 <= A[i] <= 10 ** 12

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