結果

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

ソースコード

diff #

import bisect

def find_kth_smallest():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    K = int(input[idx]); idx +=1
    P = int(input[idx]); idx +=1
    
    A = list(map(int, input[idx:idx+N]))
    idx +=N
    B = list(map(int, input[idx:idx+N]))
    idx +=N
    
    B.sort()
    
    low = 0
    high = P - 1
    ans = P - 1
    
    while low <= high:
        mid = (low + high) // 2
        total = 0
        for a in A:
            # Case 1: B_j <= mid - a (if mid >= a)
            if mid >= a:
                target = mid - a
                case1 = bisect.bisect_right(B, target)
            else:
                case1 = 0
            
            # Case 2: B_j in [P - a, mid + P - a] (clamped to P-1)
            lower = P - a
            upper = mid + P - a
            upper = min(upper, P - 1)
            
            if lower > upper:
                case2 = 0
            else:
                left = bisect.bisect_left(B, lower)
                right = bisect.bisect_right(B, upper)
                case2 = right - left
            
            total += case1 + case2
            if total > K * 2:  # Early exit if possible to avoid overflow
                break
        
        if total >= K:
            ans = mid
            high = mid - 1
        else:
            low = mid + 1
    
    print(ans)

find_kth_smallest()
0