# The Lazy Programmer

## 题目描述

A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and an executive director at the same time. The second one is a programmer. The director is so a nimble guy that the studio has already got $N$ contracts for web site development. Each contract has a deadline $d_i$.
It is known that the programmer is lazy. Usually he does not work as fast as he could. Therefore, under normal conditions the programmer needs $b_i$ of time to perform the contract number $i$. Fortunately, the guy is very greedy for money. If the director pays him $x_i$ dollars extra, he needs only $(b_i-a_ix_i)$ of time to do his job. But this extra payment does not influent other contract. It means that each contract should be paid separately to be done faster. The programmer is so greedy that he can do his job almost instantly if the extra payment is $(b_i/a_i)$ dollars for the contract number $i$.
The director has a difficult problem to solve. He needs to organize programmer’s job and, may be, assign extra payments for some of the contracts so that all contracts are performed in time. Obviously he wishes to minimize the sum of extra payments. Help the director!

## 代码

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int n, t;
double ans;
struct contract {
int a, b, d;
bool operator < (const contract &t) const {
return a < t.a;
}
} c[100001];
bool cmp(contract a, contract b) {
return a.d < b.d;
}
priority_queue<contract> q;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d", &c[i].a, &c[i].b, &c[i].d);
}
sort(c + 1, c + n + 1, cmp);
for (int i = 1; i <= n; i++) {
q.push(c[i]);
t += c[i].b;
if (t > c[i].d) {
int delta = t - c[i].d;
while (delta) {
contract tmp = q.top();
q.pop();
if (tmp.b < delta) {
delta -= tmp.b;
ans += tmp.b * 1.0 / tmp.a;
} else {
tmp.b -= delta;
ans += delta * 1.0 / tmp.a;
delta = 0;
q.push(tmp);
}
}
t = c[i].d;
}
}
printf("%.2lf\n", ans);
return 0;
}


418 I'm a teapot