链接:https://ac.nowcoder.com/acm/contest/369/B
题意:
小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务
第 i 个任务需要花费
xixi 的时间,同时完成第 i 个任务的时间不能晚于 yiyi ,时间掌控者向小A提出了一个条件:如果完成第 i 个任务的时间本应是 t ,但小A支付 m 个金币的话,他可以帮助小A在 t−m×zit−m×zi 时刻完成第 i 个任务, zizi 是时间参数,会在输入中给出
小A想按时完成所有任务,请你帮他制定一个花费金币最少的方案
注意:不能使得某个任务的花费时间小于 0 ,花费的金币可以不是整数
思路:
按照截至时间排序,没选到一个将其压入一个优先队列,以z优先原则。
当世时间不够时,选择z最大的来花费。同时每个有最大的花费额,当最大花费时间用完时,就不能使用。
//优先队列的结构体排序与sort相反。that为优先。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 10;
struct Node
{
int _x;
int _y;
double _z;
bool operator < (const Node & that) const {
return this->_y < that._y;
}
}node[MAXN];
struct Eage
{
int _x;
int _z;
bool operator < (const Eage & that) const {
return this->_z < that._z;
}
Eage(int x, int z):_x(x), _z(z) {};
};
priority_queue<Eage> que;
int main()
{
int n;
cin >> n;
for (int i = 1;i <= n;i++)
cin >> node[i]._z >> node[i]._x >> node[i]._y;
sort(node + 1, node + 1 + n);
double res = 0;
int w = 0;
for (int i = 1;i <= n;i++)
{
que.emplace(node[i]._x, node[i]._z);
w += node[i]._x;
while (w > node[i]._y)
{
Eage now = que.top();
que.pop();
int time = w - node[i]._y;
time = min(time, now._x);
w -= time;
now._x -= time;
res += 1.0 * time / now._z;
if (now._x > 0)
que.push(now);
}
}
printf("%.1lf", res);
return 0;
}