生產某種產品有N道工序,對於工序i,有S[i]和T[i]兩類機器可供選擇,機器S[i]單價為P[i],每臺每天能處理A[i]件;機器T[i]單價為Q[i],每臺每天能處理B[i]件。在不超預算X的前提下,每天最多能生產多少件產品?
1<=N<=100; 1<=A[i],B[i]<=100; 1<=P[i],Q[i],X<=1E7
分析:最大產能為所有工序的最小值,可以二分答案,由於A[i],B[i]較小,可以按A[i],B[i]來列舉。
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
i64 N, X;
std::cin >> N >> X;
std::vector<i64> A(N + 1), B(N + 1), P(N + 1), Q(N + 1);
for (int i = 1; i <= N; i++) {
std::cin >> A[i] >> P[i] >> B[i] >> Q[i];
}
auto check = [&](i64 u) {
i64 cost = 0;
for (int i = 1; i <= N; i++) {
i64 t = 1E9;
for (int j = 0; j < A[i]; j++) {
i64 k = std::max(0LL, u - j * B[i] + A[i] - 1) / A[i];
t = std::min(t, j * Q[i] + k * P[i]);
}
for (int j = 0; j < B[i]; j++) {
i64 k = std::max(0LL, u - j * A[i] + B[i] - 1) / B[i];
t = std::min(t, j * P[i] + k * Q[i]);
}
cost += t;
if (cost > X) {
return false;
}
}
return true;
};
i64 lo = 0, hi = 1E9, mid;
while (lo < hi) {
mid = lo + (hi - lo + 1) / 2;
if (check(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
}
std::cout << lo << "\n";
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}