Description
Farmer John has too much work to do,!!!!!!!!. In order for the farm to run efficiently, he had to make money from his work and spend a unit of time on every job. His workday starts from 0 hours and has 1 billion units of time (!). )。 At any one time, he can select any of the 1~n's n (1 <= n <= 100000) jobs to complete. Because he can only do one job per unit of time, and each job has a deadline, so it is difficult for him to have time to complete all the n jobs, although it is still possible. For the I work, there is a cutoff time of d_i (1 <= d_i <= 1000000000), and if he can finish the job then he can make a profit p_i (1<=p_i<=1000000000). What is the maximum profit a FJ can earn under a given job profit and cut-off time? The answer may exceed the 32-bit integer type.
Input
Line 1th: an integer n. Line 2~n+1: line i+1 has two integers separated by a space: D_i and P_i.
Output
The output line has an integer representing the maximum profit value.
Sample Input3
2 10
1 5
1 7
Sample Output17
Type: Time (spend) is fixed value 1, the remuneration is different, but because of the time limit, so in order to meet the limitations of the maximum benefit is our goal.
So, for each job (after the order of deadlines), we choose a job if it can be done and the most profitable, then try to do him.
If the number of jobs that have been selected is >d_i, indicating that there is no time to do the current work, so we would like to choose from the previous work, choose a minimum income to throw away, with the current work to fill that position;
Otherwise, then direct election he will be OK.
Code:
#include <iostream>#include<cstdio>#include<queue>#include<algorithm>#defineN 100010using namespacestd;structuse{intStLong Longv;} P[n];p riority_queue<Long Long>Q;BOOLCMP (use A,use b) {returna.st<B.st;}Long Longans;intMain () {intN,now (0); scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d%lld",&p[i].st,&p[i].v); Sort (P+1, p+n+1, CMP); for(intI=1; i<=n;i++) {ans+=p[i].v;now++;q.push (-p[i].v); if(Now>p[i].st) {ans+=q.top (); Q.pop (); now--;}} cout<<ans<<Endl;}
(Explain the code) (although it is someone else's, but it is really clever)
{
The first is sorted by the cut-off time in ascending order.
Now says how many jobs have been selected.
Each time a job comes, throw it into the heap, indicating that it has been chosen.
(But the reality is, not all of them can be chosen)
So there is a delete operation
When the number of jobs that have been selected > his date.
Shows that there is no time to do the current job, so we choose a minimum profit to throw away (the code may also be himself)
(There may be a question: would it not be lawful to throw away a least-profitable one and not be able to do it?) )
(But the answer is legal.) Because the current cut-off time is the latest deadline for the work that >= has chosen, it must be legal to discard one and replace it with the current. (because it is legal to choose the one that is the latest in the cutoff time))
}
Greedy Title 1