From a4b9ee9d4a5878249bb940d19d78ca37dc477517 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 27 Jan 2024 12:59:57 +0800 Subject: [PATCH] 20240127 Finished --- src/problem/mod.rs | 3 +- src/problem/p2861_maximum_number_of_alloys.rs | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2861_maximum_number_of_alloys.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 2998bfb..fcfa8d4 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -28,4 +28,5 @@ mod p670_maximum_swap; mod p2765_longest_alternating_subarray; mod p2865_beautiful_towers_i; mod p2859_sum_of_values_at_indices_with_k_set_bits; -mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree; \ No newline at end of file +mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree; +mod p2861_maximum_number_of_alloys; \ No newline at end of file diff --git a/src/problem/p2861_maximum_number_of_alloys.rs b/src/problem/p2861_maximum_number_of_alloys.rs new file mode 100644 index 0000000..066b351 --- /dev/null +++ b/src/problem/p2861_maximum_number_of_alloys.rs @@ -0,0 +1,75 @@ +/** + * [2861] Maximum Number of Alloys + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + fn check(n: usize, count: i32, budget: i32, composition: &Vec, stock: &Vec, cost: &Vec) -> bool { + let mut need = 0i64; + + for i in 0..n { + let number = composition[i] * count - stock[i]; + + if number > 0 { + need += number as i64 * cost[i] as i64; + } + } + + need <= budget as i64 + } + + pub fn max_number_of_alloys(n: i32, k: i32, budget: i32, + composition: Vec>, stock: Vec, cost: Vec) -> i32 { + let mut result = 0; + let (n, k) = (n as usize, k as usize); + + for m in 0..k { + let mut left = 0; + let mut right = vec![0;n]; + + for i in 0..n { + right[i] = (stock[i] + budget / cost[i]) / composition[m][i]; + } + + let mut right = *right.iter().max().unwrap(); + + while left < right { + let mid = (left + right) / 2; + + if Solution::check(n, mid, budget, &composition[m], &stock, &cost) { + left = mid + 1; + } else { + right = mid; + } + + dbg!(left, right); + } + + if !Solution::check(n, left, budget, &composition[m], &stock, &cost) { + left = left - 1; + } + + result = result.max(left); + } + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2861() { + assert_eq!(Solution::max_number_of_alloys(3,2,15, + vec![vec![1,1,1], vec![1,1,10]], + vec![0,0,0], + vec![1,2,3]), + 2); + } +}