結果

問題 No.2438 Double Least Square
ユーザー gew1fw
提出日時 2025-06-12 18:05:10
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,071 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,700 KB
実行使用メモリ 77,640 KB
最終ジャッジ日時 2025-06-12 18:06:26
合計ジャッジ時間 3,387 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
h = float(input())
points = [tuple(map(int, input().split())) for _ in range(n)]

a1 = 0.0
a2 = 0.0
prev_assigned_f = None

while True:
    assigned_f = []
    for x, y in points:
        residual_f = (y - (a1 * x + h)) ** 2
        residual_g = (y - (a2 * x)) ** 2
        assigned_f.append(residual_f < residual_g)
    
    if prev_assigned_f == assigned_f:
        break
    prev_assigned_f = assigned_f.copy()
    
    sum_xy_f = 0.0
    sum_x2_f = 0.0
    sum_xy_g = 0.0
    sum_x2_g = 0.0
    
    for i in range(n):
        x, y = points[i]
        if assigned_f[i]:
            sum_xy_f += x * (y - h)
            sum_x2_f += x * x
        else:
            sum_xy_g += x * y
            sum_x2_g += x * x
    
    new_a1 = sum_xy_f / sum_x2_f if sum_x2_f != 0 else 0.0
    new_a2 = sum_xy_g / sum_x2_g if sum_x2_g != 0 else 0.0
    
    a1, a2 = new_a1, new_a2

total = 0.0
for x, y in points:
    residual_f = (y - (a1 * x + h)) ** 2
    residual_g = (y - (a2 * x)) ** 2
    total += min(residual_f, residual_g)

print("{0:.20f}".format(total))
0