标签:des style color io os ar strong for art
417. The lazy programmerProblem code: LAZYPROG |
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 di.
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 bi of time to perform the contract number i. Fortunately, the guy is very greedy for money. If the director pays him xi dollars extra, he needs only (bi - ai*xi) of time to do his job. But this extra payment does not influence other contracts. This 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 (bi/ai) 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!
First line of the input contains an integer t (1 ≤ t ≤ 45), equal to the number of testcases. Then descriptions of t testcases follow.
First line of description contains the number of contracts N (1 ≤ N ≤ 100000, integer). Each of the next Nlines describes one contract and contains integer numbers ai, bi, di (1 ≤ ai, bi ≤ 10000; 1 ≤ di ≤1000000000) separated by spaces.
At least 90% of testcases will have 1 ≤ N ≤ 10000.
For each testcase in the input your program should output one line with a single real number S. Here S is the minimum sum of money which the director needs to pay extra so that the programmer could perform all contracts in time. The number must have two digits after the decimal point.
Input: 1 2 20 50 100 10 100 50 Output: 5.00
题意:有n个任务,每一个任务都有deadline,以及完成所需要的时间b,还有一个参数a,如果花x,那么他所需要的时间就变成b-x*a。任务需要一个接一个完成。问保证每个任务在deadline之前完成所需要花费的钱数
思路:首先确定的是deadline小的先处理。当发现curT+b[i] > deadline[i]的时候,只要从前面取出a[j]最大的,然后把curT往前挪。a[j]就用优先队列维护。
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> #include <set> #include <map> #include <cmath> using namespace std; typedef long long LL; #define REP(_,a,b) for(int _ = (a); _ <= (b); _++) const int maxn = 100000+10; const double eps = 1e-8; int dcmp(double x) { if(fabs(x) < eps) return 0; else if(x > 0) return 1; else return -1; } struct Programer{ double last,deadline,a,cost; Programer(double last=0,double deadline=0,double a=0):last(last),deadline(deadline),a(a){} friend bool operator < (Programer a,Programer b) { return a.a < b.a; } }P[maxn]; bool cmp(Programer a,Programer b) { if(a.deadline != b.deadline) return a.deadline < b.deadline; else return a.last < b.last; } int n; priority_queue<Programer> pqP; void init() { while(!pqP.empty()) pqP.pop(); } void input() { REP(i,1,n) { scanf("%lf%lf%lf",&P[i].a,&P[i].last,&P[i].deadline); P[i].cost = 0.0; } sort(P+1,P+1+n,cmp); } void solve() { double curT = 0,ret = 0; REP(i,1,n) { pqP.push(P[i]); if(dcmp(curT+P[i].last-P[i].deadline)>0) { while(dcmp(curT+P[i].last-P[i].deadline)>0) { Programer tmp = pqP.top(); pqP.pop(); double extra = (tmp.last/tmp.a-tmp.cost)*tmp.a; double need = curT+P[i].last-P[i].deadline; if(dcmp(need-extra) >= 0) { ret += tmp.last/tmp.a-tmp.cost; curT -= extra; }else { curT -= need; tmp.cost += need/tmp.a; ret += need/tmp.a; pqP.push(tmp); } } curT = P[i].deadline; }else { curT += P[i].last; } } printf("%.2f\n",ret); } int main(){ int ncase; cin >> ncase; while(ncase--) { scanf("%d",&n); init(); input(); solve(); } return 0; }
SPOJ 417 The lazy programmer(贪心)
标签:des style color io os ar strong for art
原文地址:http://blog.csdn.net/mowayao/article/details/39317521