結果
| 問題 |
No.2438 Double Least Square
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-08-19 21:36:46 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,671 bytes |
| コンパイル時間 | 14,512 ms |
| コンパイル使用メモリ | 378,900 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-29 17:46:19 |
| 合計ジャッジ時間 | 19,274 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 10 WA * 20 |
ソースコード
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 = 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 = 200;
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));
}