結果
| 問題 |
No.2438 Double Least Square
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 19:55:47 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,568 bytes |
| コンパイル時間 | 178 ms |
| コンパイル使用メモリ | 81,996 KB |
| 実行使用メモリ | 70,524 KB |
| 最終ジャッジ日時 | 2025-06-12 19:56:40 |
| 合計ジャッジ時間 | 2,770 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 WA * 11 |
ソースコード
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
n = int(data[idx])
idx += 1
H = int(data[idx])
idx += 1
points = []
for _ in range(n):
x = int(data[idx])
y = int(data[idx + 1])
points.append((x, y))
idx += 2
a1 = 0.0
a2 = 0.0
epsilon = 1e-8
max_iter = 10000
for _ in range(max_iter):
Sf = []
Sg = []
for x, y in points:
err_f = (y - a1 * x - H) ** 2
err_g = (y - a2 * x) ** 2
if err_f <= err_g:
Sf.append((x, y))
else:
Sg.append((x, y))
sum_xy_f = 0.0
sum_x2_f = 0.0
for x, y in Sf:
sum_xy_f += x * (y - H)
sum_x2_f += x * x
if sum_x2_f != 0:
a1_new = sum_xy_f / sum_x2_f
else:
a1_new = a1
sum_xy_g = 0.0
sum_x2_g = 0.0
for x, y in Sg:
sum_xy_g += x * y
sum_x2_g += x * x
if sum_x2_g != 0:
a2_new = sum_xy_g / sum_x2_g
else:
a2_new = a2
if abs(a1_new - a1) < epsilon and abs(a2_new - a2) < epsilon:
a1 = a1_new
a2 = a2_new
break
a1 = a1_new
a2 = a2_new
total = 0.0
for x, y in points:
err_f = (y - a1 * x - H) ** 2
err_g = (y - a2 * x) ** 2
total += min(err_f, err_g)
print("{0:.15f}".format(total))
if __name__ == "__main__":
main()
gew1fw