結果
問題 | No.2438 Double Least Square |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:47:37 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,173 bytes |
コンパイル時間 | 493 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 77,728 KB |
最終ジャッジ日時 | 2025-03-26 15:48:40 |
合計ジャッジ時間 | 3,767 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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.0a2 = 0.0epsilon = 1e-12max_iterations = 1000for _ in range(max_iterations):f_group = []g_group = []for x, y in points:error_f = (y - (a1 * x + h)) ** 2error_g = (y - (a2 * x)) ** 2if error_f <= error_g:f_group.append((x, y))else:g_group.append((x, y))sum_xy_f = sum(x * (y - h) for x, y in f_group)sum_x_sq_f = sum(x * x for x, y in f_group)a1_new = a1 if sum_x_sq_f == 0 else sum_xy_f / sum_x_sq_fsum_xy_g = sum(x * y for x, y in g_group)sum_x_sq_g = sum(x * x for x, y in g_group)a2_new = a2 if sum_x_sq_g == 0 else sum_xy_g / sum_x_sq_gif abs(a1_new - a1) < epsilon and abs(a2_new - a2) < epsilon:a1, a2 = a1_new, a2_newbreaka1, a2 = a1_new, a2_newtotal = 0.0for x, y in points:error_f = (y - (a1 * x + h)) ** 2error_g = (y - (a2 * x)) ** 2total += min(error_f, error_g)print("{0:.20f}".format(total).rstrip('0').rstrip('.') if '.' in "{0:.20f}".format(total) else "{0:.20f}".format(total))