Test instructions: Given n jobs, it is known that each job will take the time Q and the cut-off time D, asking you how many jobs you can do up to a maximum of one job at a time.
Analysis: This problem is greedy, should be able to see, the key is greedy strategy is what, so think, first by the cut-off time sort, then so, all the work is in accordance with the deadline row, because we first guarantee that,
Cut-off time before the first election, and then from all the traversal, the use of priority queue, Q big priority, and then consider, the back, if the back can also be completed within the deadline, put, if not, then,
and the queue q the longest ratio, if it is longer than the queue Q the longest, then do not, otherwise, then delete the longest, put it in, think why, because, if so, then the number of work does not decrease,
But there is more time left to finish the rest. If you do not know the priority queue Click Http://www.cnblogs.com/dwtfukgv/articles/5640285.html
Note that there was a pit that I had been in for several days .... That is the Q may be bigger than D, this is not to be considered .....
The code is as follows:
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <set > #include <cstring> #include <cmath> #include <queue>using namespace std;const int maxn = 8e5 + 5;stru CT node{int q, D; BOOL operator < (const node &P) const{//Priority Queue Maximum priority return Q < P.Q; }};node a[maxn];int n;bool CMP (const node &P, const node &QQ) {//Sort, return by cutoff time P.D < QQ.D | | (P.D = = qq.d && p.q < QQ.Q);} int solve () {priority_queue<node> PQ; int s = 0; for (int i = 0; i < n; ++i) if (a[i].q <= a[i].d)//Note that the work itself is problematic if (Pq.empty ()) {Pq.push (a[i]); s = a[i].q; } else if (S + a[i].q <= a[i].d) {s + = A[I].Q; Pq.push (A[i]); }//can be completed else{node U = pq.top () before the cutoff time; Pq.pop (); if (u.q > a[i].q && s-u.q + a[i].q <= a[i].d) {Pq.push (a[i]); S-= U.Q-A[I].Q; }//q the longest small else pq.push (U) than the queue; } return Pq.size ();}int Main () {//Freopen ("In.txt", "R", stdin); int T; Cin >> T; while (t--) {scanf ("%d", &n); for (int i = 0; i < n; ++i) scanf ("%d%d", &A[I].Q, &A[I].D); Sort (A, a+n, CMP);//Sort printf ("%d\n", Solve ()); if (T) printf ("\ n"); } return 0;}
UVa 1153 Keep the Customer satisfied (greedy + priority queue)