Atcoder ABC137D:Summer Vacation(贪心)

D - Summer Vacation


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of Bi after Ai days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

  • All values in input are integers.
  • 1N105
  • 1M105
  • 1Ai105
  • 1Bi104

Input

Input is given from Standard Input in the following format:

N  M
A1 B1
A2 B2

AN BN

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

  • Take and complete the first job today. You will earn the reward of 3 after four days from today.
  • Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

题意

一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在第Ai天拿到Bi的工资,问M天内最多可以拿到多少工资

思路

贪心,按照领取工资间隔升序排序

设置一个优先队列用来储存当前可以做的任务并且可以在M天内拿到工资的任务的钱数,每次取可以拿到的最大值

代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=1e6+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 struct wzy
12 {
13     int day,money;
14 }p[maxn];
15 bool cmp(wzy u,wzy v)
16 {
17     return u.day<v.day;
18 }
19 int main(int argc, char const *argv[])
20 {
21     ios::sync_with_stdio(false);
22     cin.tie(0);
23     int n,m;
24     cin>>n>>m;
25     for(int i=0;i<n;i++)
26         cin>>p[i].day>>p[i].money;
27     sort(p,p+n,cmp);
28     priority_queue<int>que;
29     int ans=0;
30     int pos=0;
31     for(int i=1;i<=m;i++)
32     {
33         while(p[pos].day<=i&&pos<n)
34             que.push(p[pos++].money);
35         if(!que.empty())
36             ans+=que.top(),que.pop();
37     }
38     cout<<ans<<endl;
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/Friends-A/p/11340019.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值