fn main() { input! { p: f64, a: [(f64, f64); 3], } let iter = 500; let inf = 10f64.powi(18); let norm = |a: (f64, f64), b: (f64, f64)| -> f64 { ((a.0 - b.0).abs().powf(p) + (a.1 - b.1).abs().powf(p)).powf(1.0 / p) }; // y, diff let f = |x: f64, p: usize, q: usize| -> (f64, f64) { let mut l = -inf; let mut r = inf; if norm(a[p], (x, l)) > norm(a[q], (x, l)) { std::mem::swap(&mut l, &mut r); } for _ in 0..iter { let m = (l + r) * 0.5; if norm(a[p], (x, m)) <= norm(a[q], (x, m)) { l = m; } else { r = m; } } let y = (l + r) * 0.5; let d = norm(a[p], (x, y)) - norm(a[3 ^ p ^ q], (x, y)); (y, d) }; let mut p = (3, 3); for i in 0..3 { for j in 0..3 { if i != j && f(-inf, i, j).1 > 0.0 && f(inf, i, j).1 < 0.0 { p = (i, j); } } } let mut l = -inf; let mut r = inf; for _ in 0..iter { let m = (l + r) * 0.5; if f(m, p.0, p.1).1 > 0.0 { l = m; } else { r = m; } } let x = (l + r) * 0.5; println!("{:.9} {:.9}", x, f(x, p.0, p.1).0); } fn ternary_search(mut l: f64, mut r: f64, iter: usize, f: F) -> f64 where F: Fn(f64) -> f64, { let inv = 1.0f64 / 3.0; for _ in 0..iter { let ll = (l + l + r) * inv; let rr = (l + r + r) * inv; if f(ll) <= f(rr) { r = rr; } else { l = ll; } } (l + r) * 0.5 } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ----------