Work Scheduling (go back with greedy)

https://www.luogu.org/problem/P2949

 

Title Description

Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.
His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 <= N <= 100,000) jobs conveniently numbered 1..N for work to do. It is possible but extremely unlikely that he has time for all N jobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.
Job i has deadline Di (1 <= Di <= 1,000,000,000). If he finishes job i by then, he makes a profit of Pi (1 <= Pi <= 1,000,000,000).
What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.

Enter a description:

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains two space-separated integers: Di and Pi

Output Description:

* Line 1: A single number on a line by itself that is the maximum possible profit FJ can earn.

Entry

3 
2 10 
1 5 
1 7 

Export

17

Explanation

Complete job 3 (1,7) at time 1 and complete job 1 (2,10) at time 2 to maximize the earnings (7 + 10 -> 17).

 

 

 

The same as the cost of each item, but different values. So it is easy to imagine, in the case of meet the limit, we will choose the largest possible value of goods.

Can we use a backpack to achieve it, the answer is no, or I do not QwQ

Well, let's look greedy

First idea is sorted according to the time (not that 1e8 ah, is the cutoff date), then if there is time to do a job, they do first it (sounds strange), then it's worth pressed into a small heap root .

When we do not find it worth a higher top of the heap than the current work, we give up that minimal work, it's time to do do with this higher-value work.

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <math.h>
10 const int INF=0x3f3f3f3f;
11 typedef long long LL;
12 const int mod=1e9+7;
13 const double PI=acos(-1);
14 const int maxn=1e5+10;
15 using namespace std;
16 
17 struct node
18 {
19     int t;
20     int m;
21 }a[100010];
22 
23 LL ans;
24 priority_queue<int ,vector<int>,greater<int> >q;
25 bool cmp(node a,node b)
26 {
27     return a.t<b.t;
28 }
29 
30 int main()
31 {
32     int n;
33     scanf("%d",&n);
34     for(int i=1;i<=n;i++)
35     {
36         scanf("%d %d",&a[i].t,&a[i].m);
37     }
38     sort(a+1,a+1+n,cmp);
39     for(int i=1;i<=n;i++)
40     {
41         if(a[i].t<=q.size())
42         {
43             if(a[i].m>q.top())
44             {
45                 ans-=q.top();
46                 q.pop();
47                 q.push(a[i].m);
48                 ans+=a[i].m;
49             }
50         }
51         else
52         {
53             q.push(a[i].m);
54             ans+=a[i].m;
55         }
56     }
57     printf("%lld\n",ans);
58     return 0;
59 }

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11345563.html