fn getline() -> String { let mut __ret = String::new(); std::io::stdin().read_line(&mut __ret).ok(); return __ret.trim_end().to_string(); } struct Input { _n: usize, h: usize, p: Vec<(usize, usize)>, } fn input() -> Input { let n = getline().parse::().unwrap(); let h = getline().parse::().unwrap(); // let h = getline().parse::().unwrap(); let mut p = Vec::new(); for _ in 0..n { let s = getline(); let v = s .split(' ') .map(|v| v.parse::().unwrap()) .collect::>(); p.push((v[0], v[1])); } Input { _n: n, h, p } } fn l(Input { _n: _, h, p }: &Input, a1: f64, a2: f64) -> f64 { let f = |x: f64| a1 * x + (*h as f64); let g = |x: f64| a2 * x; p.iter() .map(|(xi, yi)| { let f_diff = ((*yi as f64) - f((*xi) as f64)).powi(2); let g_diff = ((*yi as f64) - g((*xi) as f64)).powi(2); f_diff.min(g_diff) }) .sum() } fn newton(input: &Input, x: f64, y: f64) -> (f64, f64) { let l_x = (l(input, x + H, y) - l(input, x - H, y)) / (2.0 * H); let l_y = (l(input, x, y + H) - l(input, x, y - H)) / (2.0 * H); let l_xx = (l(input, x + H, y) - 2.0 * l(input, x, y) + l(input, x - H, y)) / (H.powi(2)); let l_yy = (l(input, x, y + H) - 2.0 * l(input, x, y) + l(input, x, y - H)) / (H.powi(2)); let l_xy = (l(input, x + H, y) + l(input, x - H, y) + l(input, x, y + H) + l(input, x, y - H) - 2.0 * l(input, x, y) - l(input, x + H, y + H) - l(input, x - H, y - H)) / (2.0 * H.powi(2)); let l_yx = l_xy; let k = l_xx * l_yy - l_xy * l_yx; if k == 0.0 { return (x, y); } let new_x = x - (1.0 / k) * (l_yy * l_x - l_xy * l_y); let new_y = y - (1.0 / k) * (-l_yx * l_x + l_xx * l_y); (new_x, new_y) } const H: f64 = 0.01; const N: usize = 10; const IN: usize = 2000; const R: f64 = 10.0; fn def_init_value(input: &Input) -> (f64, f64) { // let a = [[(0.0, 0.0); IN]; IN]; let h = 2.0 * R / IN as f64; let (mut a1, mut a2, mut res) = (0.0, 0.0, 1000000.0); for i in 0..IN { for j in 0..IN { let tmp_a1 = -R + h * i as f64; let tmp_a2 = -R + h * j as f64; let tmp_res = l(input, tmp_a1, tmp_a2); if res > tmp_res { (a1, a2, res) = (tmp_a1, tmp_a2, tmp_res) } } } (a1, a2) } fn main() { let i = input(); let (mut a1, mut a2) = def_init_value(&i); for _ in 0..N { (a1, a2) = newton(&i, a1, a2); } println!("{:?}", l(&i, a1, a2)); }