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 = 1.0 / (l_xx * l_yy - l_xy * l_yx); let new_x = x - k * (l_yy * l_x - l_xy * l_y); let new_y = y - k * (-l_yx * l_x + l_xx * l_y); (new_x, new_y) } const H: f64 = 0.01; const N: usize = 10; fn main() { let i = input(); let (mut a1, mut a2) = (-0.5, 0.5); for _ in 0..N { (a1, a2) = newton(&i, a1, a2) } println!("{:?}", l(&i, a1, a2)); }