https://atcoder.jp/contests/abc304/tasks/abc304_d
二分木を作って、イチゴが一つになるかもう分割できなくなるまで分割します。
Rustで二分木を作るのは大変ですね。
// A Piece of Cake #![allow(non_snake_case)] use std::cmp::{min, max}; //////////////////// 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() } //////////////////// Node //////////////////// type Point = (usize, usize); struct Node { num: usize, child1: Option<Box<Node>>, child2: Option<Box<Node>>, } impl Node { fn new(points: Vec<Point>, a: Vec<usize>, b: Vec<usize>) -> Node { let num = points.len(); if points.len() <= 1 || (a.is_empty() && b.is_empty()) { Node { num, child1: None, child2: None } } else if a.len() >= b.len() { let mid = (a.len() - 1) / 2; let ax = a[mid]; let mut points1: Vec<Point> = Vec::new(); let mut points2: Vec<Point> = Vec::new(); for pt in points.into_iter() { if pt.0 < ax { points1.push(pt) } else { points2.push(pt) } } let a1: Vec<usize> = (0..mid).map(|i| a[i]).collect(); let a2: Vec<usize> = (mid+1..a.len()).map(|i| a[i]).collect(); let child1 = Node::new(points1, a1, b.to_vec()); let child2 = Node::new(points2, a2, b); Node { num, child1: Some(Box::new(child1)), child2: Some(Box::new(child2)) } } else { let mid = (b.len() - 1) / 2; let by = b[mid]; let mut points1: Vec<Point> = Vec::new(); let mut points2: Vec<Point> = Vec::new(); for pt in points.into_iter() { if pt.1 < by { points1.push(pt) } else { points2.push(pt) } } let b1: Vec<usize> = (0..mid).map(|i| b[i]).collect(); let b2: Vec<usize> = (mid+1..b.len()).map(|i| b[i]).collect(); let child1 = Node::new(points1, a.to_vec(), b1); let child2 = Node::new(points2, a, b2); Node { num, child1: Some(Box::new(child1)), child2: Some(Box::new(child2)) } } } fn is_leaf(&self) -> bool { self.child1.is_none() } fn min(&self) -> usize { if self.is_leaf() { self.num } else { min(self.child1.as_ref().unwrap().min(), self.child2.as_ref().unwrap().min()) } } fn max(&self) -> usize { if self.is_leaf() { self.num } else { max(self.child1.as_ref().unwrap().max(), self.child2.as_ref().unwrap().max()) } } } //////////////////// process //////////////////// fn read_point() -> Point { let v = read_vec(); (v[0], v[1]) } fn read_input() -> (Vec<Point>, Vec<usize>, Vec<usize>) { let _v: Vec<usize> = read_vec(); let N: usize = read(); let points: Vec<Point> = (0..N).map(|_| read_point()).collect(); let _A: usize = read(); let a: Vec<usize> = read_vec(); let _B: usize = read(); let b: Vec<usize> = read_vec(); (points, a, b) } fn f(points: Vec<Point>, a: Vec<usize>, b: Vec<usize>) -> (usize, usize) { let tree = Node::new(points, a, b); (tree.min(), tree.max()) } //////////////////// main //////////////////// fn main() { let (points, a, b) = read_input(); let (n, x) = f(points, a, b); println!("{} {}", n, x) }