結果

問題 No.2221 Set X
ユーザー gew1fw
提出日時 2025-06-12 20:55:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,436 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 81,724 KB
実行使用メモリ 96,384 KB
最終ジャッジ日時 2025-06-12 20:59:10
合計ジャッジ時間 7,994 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    input = sys.stdin.read().split()
    N = int(input[0])
    A = list(map(int, input[1:N+1]))
    A.sort()
    A_N = A[-1]

    def compute_f(X):
        if X == 0:
            return float('inf')
        prev_q = None
        count = 0
        for a in A:
            q = a // X
            if q != prev_q:
                count += 1
                prev_q = q
        return (X + 1) * count

    # Ternary search to narrow down the range [1, A_N]
    low = 1
    high = A_N
    best_x = low
    best_f = compute_f(low)

    # Perform ternary search steps
    for _ in range(100):
        if high - low <= 3:
            break
        mid1 = (2 * low + high) // 3
        mid2 = (low + 2 * high) // 3
        f1 = compute_f(mid1)
        f2 = compute_f(mid2)
        if f1 < f2:
            high = mid2
        else:
            low = mid1

    # Check all X in the narrowed range
    for X in range(low, high + 1):
        current_f = compute_f(X)
        if current_f < best_f or (current_f == best_f and X < best_x):
            best_f = current_f
            best_x = X

    # Check candidate X = A_N + 1
    X_candidate = A_N + 1
    f_candidate = (X_candidate + 1) * 1  # All q are 0, K=1
    if f_candidate < best_f or (f_candidate == best_f and X_candidate < best_x):
        best_f = f_candidate
        best_x = X_candidate

    print(best_x)
    print(best_f)

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