Topic Links: 51nod 1163 the highest reward
Looking at this question I immediately think of yesterday also did a greedy plus priority queue of the problem.
Sort by the late end of a task, select a task, and then, if the task's late end is later than the current point, press the reward value of the task into the queue or replace the reward value of the smallest task in the queue, prioritizing the priority queue by a small reward value.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 using namespacestd;6typedefLong Longll;7 Const intN =50001;8 structtask{9 inte;Ten intW; One }a[n]; A BOOLCMP (Task x, task y) { - returnX.E <y.e; - } thepriority_queue<int, vector<int, greater<int> >Q; - intMain () { - intN, I, J; -ll ans =0; +scanf"%d", &n); - for(i =0; I < n; ++i) +scanf"%d%d", &A[I].E, &A[I].W); ASort (A, A +N, CMP); at for(i =0; I < n; ++i) { - if(A[i].e >q.size ()) { - //if the task's late end time is later than the current point in time - Q.push (A[I].W); -Ans + =A[I].W; - } in Else{ - //may be replaced by a higher reward task toAns + =A[I].W; + Q.push (A[I].W); -Ans-=q.top (); the Q.pop (); * } $ }Panax Notoginsengprintf"%lld\n", ans); - return 0; the}View Code
51nod 1163 Highest reward (greedy + priority queue)