今度はこのシリーズをRustで解いていきます。
https://atcoder.jp/contests/typical90/tasks/typical90_a
最適な長さは直ちには見つからないけど、最短の長さを指定したら何分割になるかはすぐに分かるので、二分探索ですね。
// Yokan Party #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// process //////////////////// fn read_input() -> (usize, i32, Vec<i32>) { let v = read_vec(); let L: i32 = v[1]; let K: usize = read(); let A: Vec<i32> = read_vec(); (K, L, A) } fn divisible_num(x: i32, A: &Vec<i32>) -> usize { let mut k: usize = 0; let mut p: i32 = 0; for &a in A.iter() { if a - p >= x { p = a; k += 1 } } k } fn bin_search(first: i32, last: i32, K: usize, A: &Vec<i32>) -> i32 { if last - first == 1 { first } else { let mid = (first + last) / 2; if divisible_num(mid, A) >= K+1 { bin_search(mid, last, K, A) } else { bin_search(first, mid, K, A) } } } fn F(K: usize, L: i32, mut A: Vec<i32>) -> i32 { A.push(L); bin_search(1, L, K, &A) } fn main() { let (A, B, C) = read_input(); println!("{}", F(A, B, C)) }