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", &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