結果

問題 No.2221 Set X
ユーザー gew1fw
提出日時 2025-06-12 21:16:10
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,593 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 95,744 KB
最終ジャッジ日時 2025-06-12 21:16:50
合計ジャッジ時間 14,073 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def compute_f(X):
        if X == 0:
            return float('inf')
        count = 0
        prev = -1
        for a in A:
            current = a // X
            if current != prev:
                count += 1
                prev = current
        return (X + 1) * count

    low = 1
    high = max_A + 1

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

    # Collect candidate X values
    start = max(1, low - 100)
    end = min(high + 100, max_A + 1)
    candidates = list(range(start, end + 1))
    # Ensure we include important points
    candidates.append(1)
    candidates.append(max_A)
    candidates.append(max_A + 1)
    # Remove duplicates and sort
    candidates = list(set(candidates))
    candidates.sort()

    min_f = float('inf')
    best_x = None
    for x in candidates:
        if x < 1:
            continue  # since X must be positive
        current_f = compute_f(x)
        if current_f < min_f or (current_f == min_f and (best_x is None or x < best_x)):
            min_f = current_f
            best_x = x

    print(best_x)
    print(min_f)

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