結果

問題 No.2221 Set X
ユーザー gew1fw
提出日時 2025-06-12 18:01:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,337 bytes
コンパイル時間 411 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 192,672 KB
最終ジャッジ日時 2025-06-12 18:01:31
合計ジャッジ時間 7,242 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

n = int(input())
A = list(map(int, input().split()))
A_1 = A[0]
A_N = A[-1]

# Generate candidate X values
candidates = set()
candidates.add(1)
candidates.add(A_N)
candidates.add(A_N + 1)

# Add candidates based on A_N
max_q = int(math.isqrt(A_N)) + 1
for q in range(1, max_q + 1):
    x = A_N // q
    candidates.add(x)
    candidates.add(x - 1)
    candidates.add(x + 1)

# Add candidates based on A_1 if it's non-zero
if A_1 > 0:
    max_q_a1 = int(math.isqrt(A_1)) + 1
    for q in range(1, max_q_a1 + 1):
        x = A_1 // q
        candidates.add(x)
        candidates.add(x - 1)
        candidates.add(x + 1)

# Filter valid X values
valid_x = []
for x in candidates:
    if x > 0 and x <= A_N + 1:
        valid_x.append(x)
valid_x = list(set(valid_x))
valid_x.sort()

# Function to count distinct quotients
def count_distinct_quotients(X):
    if X == 0:
        return 0
    prev_q = A[0] // X
    count = 1
    for a in A[1:]:
        q = a // X
        if q != prev_q:
            count += 1
            prev_q = q
    return count

# Find the minimum f(X)
min_f = float('inf')
best_x = None
for x in valid_x:
    c = count_distinct_quotients(x)
    current_f = (x + 1) * c
    if current_f < min_f or (current_f == min_f and x < best_x):
        min_f = current_f
        best_x = x

print(best_x)
print(min_f)
0