思路来自こたつがめ(kotatsugame)大佬的视频。
首先,容易想到我们可以二分答案 (最大生产力),对于每一个工程令生产力总和 ,然后计算出来所需花费,判断是否 即可。
对于一个工程 ,我们需要找到两个机器的生产力 满足 的条件下,最小化 的值。
不妨使用带余除法分解 :
那么,使用顶函数的性质:
不妨记:
则:
我们考虑直接枚举 为常数并钦定后面的式子 ,
此时:
是关于 的一次函数,不妨两个端点都直接记入贡献。
然后考虑后面的式子 即:
那么:
代码:
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
using ll = long long;
#define int ll
int n, x;
struct pt {
int a, p;
int b, q;
};
vector<pt> process;
bool check(int W) {
int rest = x;
for (auto [a, p, b, q] : process) {
int cost = rest + 1;
for (int x0 = 0; x0 < b; ++x0) {
int w = (max(0ll, W - a * x0) + b - 1) / b;
int base = p * x0 + q * w;
int add = p * b - q * a;
int lx = 0, rx = w / a;
cost = min({
cost,
p * (x0 + b * (rx + 1)),
base + add * lx,
base + add * rx
});
}
if ((rest -= cost) < 0)
return false;
}
return true;
}
void Main() {
cin >> n >> x;
process.resize(n);
for (auto &[a, p, b, q] : process)
cin >> a >> p >> b >> q;
int l = 0, r = 1e9;
int ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
cout << ans << endl;
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
return Main(), 0;
}