結果

問題 No.1597 Matrix Sort
ユーザー norioc
提出日時 2024-06-13 09:46:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 635 ms / 1,500 ms
コード長 914 bytes
コンパイル時間 527 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 373,360 KB
最終ジャッジ日時 2024-06-13 09:47:15
合計ジャッジ時間 13,395 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import accumulate


def accum(a: list):
    acc = list(accumulate(a))
    return lambda l, r: acc[r] - (acc[l-1] if l > 0 else 0)


N, K, P = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

bs = [0] * (P+1)
for b in B:
    bs[b] += 1

acc_bs = accum(bs)


# A_i + B_j <= x を満たす (i, j) の個数を返す
def count(x: int) -> int:
    res = 0
    for a in A:  # A_i を固定
        # a + B_j <= x を満たす j の範囲
        if 0 <= x-a:
            res += acc_bs(0, x-a)
        # a + B_j <= P + x を満たす j の範囲
        res += acc_bs(P-a, min(P, P-a+x))

    return res


lo = 0
hi = P
ans = hi
while lo <= hi:
    m = (lo + hi) // 2
    cnt = count(m)  # A_i + B_j <= m (mod P) を満たす個数は K 個以上か?
    if cnt >= K:
        ans = min(ans, m)
        hi = m - 1
    else:
        lo = m + 1

print(ans)
0