力扣2861.最大合金数
-
对每一台机器都做一次二分答案
cpp
class Solution {
public:
int maxNumberOfAlloys(int n, int k, int budget, vector<vector<int>>& composition, vector<int>& stock, vector<int>& cost) {
int res = 0;
int mx = ranges::min(stock) + budget;
for(auto &comp : composition)
{
auto check = [&](long long num) -> bool
{
long long ans=0;
for(int i=0;i<n;i++)
if(stock[i] < comp[i] * num)
{
ans += (comp[i] * num - stock[i]) * cost[i];
if(ans > budget) return false;
}
return true;
};
int l = res,r = mx + 1;
while(l < r)
{
int mid = l + r + 1>> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
res = l;
}
return res;
}
};