Spoj 417 The lazy programmer (Greedy & amp; priority queue)

Source: Internet
Author: User
Tags integer numbers

 
 
SPOJ Problem Set (classical) 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
There are a total of n tasks, each of which has a cutoff time di and a time-consuming bi. If you spend x yuan, you can reduce the time consumption to bi-ai * x. Now we need to find a solution to complete all the tasks by the deadline with the minimum amount of money.
Idea: first sort the deadline. Then, follow the normal working hours. If the deadline is exceeded, find the maximum work of a in the priority queue and then pay for it. In this way, it must be optimal. For details, see the code:
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
# Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           // # Pragma comment (linker, "/STACK: 1024000000,1024000000") using namespace std; const int INF = 0x3f3f3f; const double eps = 1e-8; const double PI = acos (-1.0); const int maxn = 100010; typedef _ int64 ll; class yb {public: int a, B, d; double c; // The remaining time can be reduced by yb () {c = 0;} bool operator <(const yb & t) const // priority of the priority queue {return
            
              B) return 1; else return-1;} bool cmp (yb a, yb B) // sort by deadline {return a. d
             
               Q; T = ans = 0; for (I = 0; I
              
                0) {tp = q. top (); q. pop (); x = (T-con [I]. d)/tp. a; if (cmpd (double) tp. b/tp. a, x + tp. c)> 0) {tp. c + = x; ans + = x; T = con [I]. d; q. push (tp); break;} else {x = (double) tp. b/tp. a-tp.c; ans + = x; T-= x * tp. a ;}} printf ("%. 2f \ n ", ans);} return 0 ;}
              
             
            
          
         
        
       
      
     
    
   
  


Related Article

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.