結果

問題 No.1597 Matrix Sort
ユーザー gew1fw
提出日時 2025-06-12 13:31:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,141 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 116,992 KB
最終ジャッジ日時 2025-06-12 13:37:33
合計ジャッジ時間 33,524 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17 TLE * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    n = int(data[idx])
    idx += 1
    k = int(data[idx])
    idx += 1
    p = int(data[idx])
    idx += 1
    A = list(map(int, data[idx:idx+n]))
    idx += n
    B = list(map(int, data[idx:idx+n]))
    idx += n
    
    B.sort()
    
    low = 0
    high = p - 1
    
    while low < high:
        mid = (low + high) // 2
        total = 0
        for a in A:
            ci = a % p
            if mid >= ci:
                part1 = bisect.bisect_right(B, mid - ci)
                part2 = n - bisect.bisect_left(B, p - ci)
                total += part1 + part2
            else:
                lower = p - ci
                upper = mid + p - ci
                left = bisect.bisect_left(B, lower)
                right = bisect.bisect_right(B, upper)
                total += (right - left)
            if total > n * n:
                break  # Prevent overflow in case of large counts
        if total >= k:
            high = mid
        else:
            low = mid + 1
    print(low)

if __name__ == "__main__":
    main()
0