結果
問題 |
No.2438 Double Least Square
|
ユーザー |
![]() |
提出日時 | 2025-04-15 22:29:15 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,175 bytes |
コンパイル時間 | 416 ms |
コンパイル使用メモリ | 81,808 KB |
実行使用メモリ | 76,852 KB |
最終ジャッジ日時 | 2025-04-15 22:31:24 |
合計ジャッジ時間 | 3,514 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 WA * 11 |
ソースコード
n = int(input()) h = int(input()) points = [tuple(map(int, input().split())) for _ in range(n)] a1 = 0.0 a2 = 0.0 prev_assignment = None while True: current_assignment = [] for x, y in points: fx = a1 * x + h gx = a2 * x error_f = (y - fx) ** 2 error_g = (y - gx) ** 2 current_assignment.append(error_f <= error_g) if current_assignment == prev_assignment: break prev_assignment = current_assignment.copy() sum_f_x2 = 0.0 sum_f_xy = 0.0 sum_g_x2 = 0.0 sum_g_xy = 0.0 for i in range(n): x, y = points[i] if current_assignment[i]: sum_f_x2 += x * x sum_f_xy += x * (y - h) else: sum_g_x2 += x * x sum_g_xy += x * y new_a1 = a1 if sum_f_x2 != 0: new_a1 = sum_f_xy / sum_f_x2 new_a2 = a2 if sum_g_x2 != 0: new_a2 = sum_g_xy / sum_g_x2 a1 = new_a1 a2 = new_a2 total = 0.0 for x, y in points: fx = a1 * x + h gx = a2 * x error_f = (y - fx) ** 2 error_g = (y - gx) ** 2 total += min(error_f, error_g) print("{0:.20f}".format(total))