結果

問題 No.1361 [Zelkova 4th Tune *] QUADRUPLE-SEQUENCEの詩
ユーザー lam6er
提出日時 2025-03-20 20:35:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,343 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 82,612 KB
実行使用メモリ 133,776 KB
最終ジャッジ日時 2025-03-20 20:36:49
合計ジャッジ時間 9,542 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28 TLE * 1 -- * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect

def main():
    import sys
    K, L, M, N, S = map(int, sys.stdin.readline().split())
    A = list(map(int, sys.stdin.readline().split()))
    B = list(map(int, sys.stdin.readline().split()))
    C = list(map(int, sys.stdin.readline().split()))
    D = list(map(int, sys.stdin.readline().split()))
    
    # Generate AB products and sort them
    ab_prod = []
    for a in A:
        for b in B:
            ab_prod.append((a * b, a, b))
    ab_prod_sorted = sorted(ab_prod, key=lambda x: x[0])
    ab_sorted_values = [x[0] for x in ab_prod_sorted]
    
    # Generate CD products and sort them
    cd_prod = []
    for c in C:
        for d in D:
            cd_prod.append((c * d, c, d))
    cd_prod_sorted = sorted(cd_prod, key=lambda x: x[0])
    cd_sorted_values = [x[0] for x in cd_prod_sorted]
    
    # Determine initial low and high for binary search
    if not ab_sorted_values:
        ab_min = ab_max = 0
    else:
        ab_min = ab_sorted_values[0]
        ab_max = ab_sorted_values[-1]
    if not cd_sorted_values:
        cd_min = cd_max = 0
    else:
        cd_min = cd_sorted_values[0]
        cd_max = cd_sorted_values[-1]
    
    candidates = [
        ab_min * cd_min,
        ab_min * cd_max,
        ab_max * cd_min,
        ab_max * cd_max
    ]
    low = min(candidates)
    high = max(candidates)
    
    answer_T = None
    
    # Binary search to find the S-th smallest element
    while low <= high:
        mid = (low + high) // 2
        current_count = 0
        
        for ab in ab_prod_sorted:
            a_b, a_val, b_val = ab
            if a_b == 0:
                if mid >= 0:
                    current_count += len(cd_sorted_values)
                continue
            if a_b > 0:
                target = mid // a_b
                cnt = bisect.bisect_right(cd_sorted_values, target)
                current_count += cnt
            else:
                numerator = mid + a_b + 1
                target_ceil = numerator // a_b
                idx = bisect.bisect_left(cd_sorted_values, target_ceil)
                cnt = len(cd_sorted_values) - idx
                current_count += cnt
        
        if current_count >= S:
            answer_T = mid
            high = mid - 1
        else:
            low = mid + 1
    
    # Find the combination a,b,c,d
    T = answer_T
    result = None
    
    # Check all ab in AB to find the matching cd
    for ab in ab_prod_sorted:
        a_b, a_val, b_val = ab
        if a_b == 0:
            if T == 0 and len(cd_prod_sorted) > 0:
                result = (a_val, b_val, cd_prod_sorted[0][1], cd_prod_sorted[0][2])
                break
            else:
                continue
        if a_b != 0 and T % a_b != 0:
            continue
        cd_target = T // a_b
        
        idx = bisect.bisect_left(cd_sorted_values, cd_target)
        if idx < len(cd_sorted_values) and cd_sorted_values[idx] == cd_target:
            # Find the first occurrence of cd_target in cd_prod_sorted
            while idx < len(cd_prod_sorted) and cd_prod_sorted[idx][0] == cd_target:
                result = (a_val, b_val, cd_prod_sorted[idx][1], cd_prod_sorted[idx][2])
                break
            if result:
                break
    
    print(T)
    print(' '.join(map(str, result)))

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