20240127 Finished

This commit is contained in:
jackfiled 2024-01-27 12:59:57 +08:00
parent f2c7ff1715
commit a4b9ee9d4a
2 changed files with 77 additions and 1 deletions

View File

@ -29,3 +29,4 @@ mod p2765_longest_alternating_subarray;
mod p2865_beautiful_towers_i; mod p2865_beautiful_towers_i;
mod p2859_sum_of_values_at_indices_with_k_set_bits; mod p2859_sum_of_values_at_indices_with_k_set_bits;
mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree; mod p2846_minimum_edge_weight_equilibrium_queries_in_a_tree;
mod p2861_maximum_number_of_alloys;

View File

@ -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<i32>, stock: &Vec<i32>, cost: &Vec<i32>) -> 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<Vec<i32>>, stock: Vec<i32>, cost: Vec<i32>) -> 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);
}
}