結果

問題 No.1597 Matrix Sort
ユーザー gew1fw
提出日時 2025-06-12 16:39:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,241 ms / 1,500 ms
コード長 1,333 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 117,416 KB
最終ジャッジ日時 2025-06-12 16:39:54
合計ジャッジ時間 23,640 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect

def find_kth_smallest():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr +=1
    K = int(input[ptr])
    ptr +=1
    P = int(input[ptr])
    ptr +=1
    
    A = list(map(int, input[ptr:ptr+N]))
    ptr += N
    B = list(map(int, input[ptr:ptr+N]))
    ptr += N
    
    A.sort()
    B.sort()
    
    def count(x):
        count1 = 0
        j = len(B) - 1
        for a in A:
            target = x - a
            if target < 0:
                continue
            while j >= 0 and B[j] > target:
                j -= 1
            if j >= 0:
                count1 += (j + 1)
        
        count2 = 0
        for a in A:
            lower = max(P - a, 0)
            upper = min(x + P - a, P - 1)
            if lower > upper:
                continue
            left = bisect.bisect_left(B, lower)
            right = bisect.bisect_right(B, upper)
            count2 += (right - left)
        
        return count1 + count2
    
    low = 0
    high = P - 1
    answer = high  # Initialize with the maximum possible value
    
    while low <= high:
        mid = (low + high) // 2
        c = count(mid)
        if c >= K:
            answer = mid
            high = mid - 1
        else:
            low = mid + 1
    
    print(answer)

find_kth_smallest()
0