Spoj 417 the lazy programmer (Greedy)

Source: Internet
Author: User
Tags integer numbers
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 ctor at the same time. the second one is a programmer. the director is so a nimble guy that the studio has already gotNContracts for Web site development. Each contract has a deadlineDi.

It is known that the programmer is lazy. Usually he does not work as fast as he cocould. Therefore, under normal conditions the programmer needsBiOf time to perform the contract numberI. Fortunately, the guy is very greedy for money. If the director pays himXIDollars 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 shocould 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 numberI.

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 saved in time. obviusly he wishes to minimize the sum of extra payments. help the director!

Input

First line of the input contains an integerT(1T45), Equal to the number of testcases. Then descriptionsTTestcases follow.

First line of description contains the number of contractsN(1N100000, Integer). Each of the nextNLines describes one contract and contains integer numbersAI,Bi,Di(1AI,Bi10000;1Di1000000000) Separated by spaces.

At least90%Of testcases will have1N10000.

Output

For each testcase in the input your program shocould 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 cocould perform all contracts in time. the number must have two digits after the decimal point.

Example
Input:1220 50 10010 100 50Output:5.00
 
 
Question: There are n tasks, each of which has a deadline, and the time required to complete B. There is also a parameter A. If X is spent, then the time required by him is changed to B-x *. The task must be completed one by one. Q: The amount of money required to ensure that each task is completed before deadline
 
Idea: The first thing to determine is that deadline should be processed first. When you find Curt + B [I]> deadline [I], you only need to retrieve the largest a [J] from the front and move the curt forward. A [J] is maintained by the priority queue.
#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 (Greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.