题解:B4204 [常州市程序设计小能手 2021] 烧菜
容易想到维护一个小根堆
那么我们一开始在
我们给它分配任务:维护未清洗的白菜数量
但是这样做有问题。可能出现一个机器人想煮菜,但还没有菜被洗完的情况。hack 数据如下:
3 2 1000 1
这时上述算法会输出 2000,但答案为 2001。
考虑额外维护小根堆
#include <iostream>
#include <queue>
using namespace std;
int n, m, t, a, b;
int main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> a >> b, t = n, m = min(m, n);
priority_queue<int, vector<int>, greater<int>> q, p;
for (int i = 1; i <= m; ++i) q.push(0);
while (n || t) {
int tp = q.top();
q.pop();
if (n) --n, p.push(tp + a), q.push(tp + a);
else if (tp + b < p.top()) q.push(p.top());
else --t, p.pop(), q.push(tp + b);
}
int ans = 0;
for (; !q.empty(); q.pop()) ans = q.top();
cout << ans << '\n';
return 0;
}