生产某种产品有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;
}
标签:Dilemma,int,lo,mid,i64,Optimization,abc374E,return,hi
From: https://www.cnblogs.com/chenfy27/p/18450754