結果

問題 No.1597 Matrix Sort
ユーザー gew1fw
提出日時 2025-06-12 16:43:24
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,377 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 123,836 KB
最終ジャッジ日時 2025-06-12 16:43:34
合計ジャッジ時間 7,637 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other AC * 3 TLE * 3 -- * 21
権限があれば一括ダウンロードができます

ソースコード

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]))
    B.sort()
    
    def count_le(X):
        total = 0
        for a in A:
            # Case 1: B_j <= X - a and B_j <= P-1 -a
            cnt1 = 0
            if X >= a:
                max_case1 = min(X - a, (P-1) - a)
                if max_case1 >= 0:
                    cnt1 = bisect.bisect_right(B, max_case1)
            # Case 2: B_j >= P - a and B_j <= X + P - a
            low_case2 = P - a
            high_case2 = X + P - a
            high_clamped = min(high_case2, P-1)
            cnt2 = 0
            if low_case2 <= high_clamped:
                left = bisect.bisect_left(B, low_case2)
                right = bisect.bisect_right(B, high_clamped)
                cnt2 = right - left
            total += cnt1 + cnt2
        return total
    
    low = 0
    high = P - 1
    answer = high
    while low <= high:
        mid = (low + high) // 2
        cnt = count_le(mid)
        if cnt >= K:
            answer = mid
            high = mid - 1
        else:
            low = mid + 1
    print(answer)

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