結果

問題 No.2221 Set X
ユーザー lam6er
提出日時 2025-03-20 20:21:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,455 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 165,008 KB
最終ジャッジ日時 2025-03-20 20:22:44
合計ジャッジ時間 8,705 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 WA * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    A = sorted(A)  # Just in case, but input says it's already sorted
    
    max_A = A[-1]
    left = 1
    right = max_A + 1  # Cover X up to max_A +1
    
    def compute_f(X):
        if X == 0:
            return float('inf')
        prev = A[0] // X
        count = 1
        for a in A[1:]:
            current = a // X
            if current != prev:
                count += 1
                prev = current
        return (X + 1) * count
    
    # Ternary search to narrow down the range
    while right - left > 3:
        mid1 = left + (right - left) // 3
        mid2 = right - (right - left) // 3
        f1 = compute_f(mid1)
        f2 = compute_f(mid2)
        if f1 < f2:
            right = mid2
        else:
            left = mid1
    
    # Check all possible X in [left, right] inclusive
    min_f = float('inf')
    best_x = None
    for X in range(left, right + 1):
        current_f = compute_f(X)
        if current_f < min_f or (current_f == min_f and X < best_x):
            min_f = current_f
            best_x = X
    
    # Also check higher values if needed. Because when X > max_A, all elements are 0 except A >= X
    # Let's check X up to max_A +1 (included in initial range)
    # So the loop above should suffice
    
    print(best_x)
    print(min_f)

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