uva1153 Keep the Customer satisfied

Source: Internet
Author: User

Greedy Plus priority queue (default is small in front, just right)

Here is a very routine, set up in the queue are eligible to consider the new addition can be. The case of the empty queue is processed again. Very Perfect//

The cut-off time is short, in front, more dry.
Sort by due Date first
Priority queues are sorted according to the time required to complete
The first thing in the queue is that it can be done.
Strategy: Newly added, if the previous completion can still be completed, directly add in; No, because the due date in the further, does not affect the other, the comparison of the top element with the time, shorter to replace (at this time because the deadline, the use of time is short, so can be established)

#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;Const intMAXN =800005;Const intN =2000005;structorder{intQ, D; BOOL operator< (ConstOrder &a)Const {        returnq<a.q;}} O[MAXN];intCMP (Order A, order B) {returnA.D <B.D;}intN;intsolve () {priority_queue<int>Done ; intsum =0, temp;  for(inti =0; I < n; i++) {        if(O[i].q + sum <=o[i].d)            {Done.push (O[I].Q); Sum+=o[i].q; }        Else if(!Done.empty ()) {Temp=Done.top (); if(Temp >o[i].q) {Sum= Sum-temp +o[i].q;                Done.pop ();            Done.push (O[I].Q); }        }    }    returndone.size ();}intMain () {intT; scanf ("%d", &T);  while(t--) {scanf ("%d", &N);  for(inti =0; I < n; i++) scanf ("%d%d", &AMP;O[I].Q, &O[I].D); Sort (o, O+N, CMP); printf ("%d\n", Solve ()); if(T) printf ("\ n"); }    return 0;}

uva1153 Keep the Customer satisfied

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.