AtCoder Beginner Contest 137 D题【贪心】

【题意】一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在这一天之后的(Ai-1)天拿到Bi的工资,问M天内最多可以拿到多少工资。

链接:https://atcoder.jp/contests/abc137/tasks/abc137_d

【思路】按时间将任务排序,把 ii 时刻能做的任务都扔进堆里 , 因为任务是一次性的,所以每个任务放进去过就不再放入了。然后每个时刻看一下堆里面有没有数,有的话就取堆顶出来,而且每个时刻只能取一次。正确性显然。

代码:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define int long long
 6 struct str{
 7     int day,money;
 8 }st[120000];
 9 priority_queue<int> q;
10 bool cmp(str a,str b){
11     return a.day<b.day;
12 }
13 signed main(){
14     int n,m;
15     cin>>n>>m;
16     for(int i=1;i<=n;i++)
17         cin>>st[i].day>>st[i].money;
18     int ans=0;
19     int now=1;
20     sort(st+1,st+1+n,cmp);
21     for(int i=1;i<=m;i++){
22         while(st[now].day<=i&&now<=n){
23             q.push(st[now++].money);
24         }
25         if(!q.empty()){
26             ans+=q.top();
27             q.pop();
28         }
29     }
30     cout<<ans;
31     return 0;
32 } 
33 //

猜你喜欢

转载自www.cnblogs.com/pengge666/p/11564230.html