結果
問題 |
No.2438 Double Least Square
|
ユーザー |
|
提出日時 | 2023-08-19 19:44:39 |
言語 | Rust (1.83.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,061 bytes |
コンパイル時間 | 22,176 ms |
コンパイル使用メモリ | 378,952 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-29 15:02:54 |
合計ジャッジ時間 | 14,532 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 WA * 1 |
other | AC * 3 WA * 27 |
ソースコード
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::<usize>().unwrap(); let h = getline().parse::<usize>().unwrap(); // let h = getline().parse::<usize>().unwrap(); let mut p = Vec::new(); for _ in 0..n { let s = getline(); let v = s .split(' ') .map(|v| v.parse::<usize>().unwrap()) .collect::<Vec<_>>(); 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)); }