贪心算法例题

Summer Vacation(贪心算法)

题目:[https://vjudge.net/contest/327427#problem/F]
(https://vjudge.net/contest/327427#problem/F)

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i 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.
Input
Input is given from Standard Input in the following format:
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.
Sample Input 2
5 3
1 2
1 3
1 4
2 1
2 3
Sample Output 2
10
Sample Input 3
1 1
2 1
Sample Output 3
0

优先队列: priority_queue<int>que;
优先队列默认从大到小排列

如果想要队列中的元素从小到大排列
则需要建立一个struct
struct node{
	int x,y;
	friend bool operator<(ndoe a,node b)
	{
		return a.x>b.y;
	}
};

解析:
输入地方需要加注意:如果当前的这个工作所需要的天数大于m 直接不存
其他的都存到vector数组
vector数组储存数据为了方便得到相同的时间得到的不同的报酬
并且储存到优先队列之后就得到相同时间得到的不同的报酬进行从大到小的排列

#include<iostream>
#include<algorithm>
#include<queue>
typedef long long ll;
using namespace std;
const int maxn = 1e5+10;
vector<int>ve[maxn];
//如果想要队列 从小到大排列 
//struct node{
//	int x,int y;
//	friend bool operator<(node a,node b)
//	{
//		return a.x>b.x;
//	}
//};
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	cin>>n>>m;
	int a,b;
	for(int i=1;i<=n;i++)
	{
		cin>>a>>b;
		if(a<=m)
		{
			ve[m-a+1].push_back(b);
		}
	}
	ll ans = 0;
	priority_queue<int>que;//队列从小到大飘排列 
	for(int i=m;i>=1;i--)
	{
		for(auto v:ve[i])
			que.push(v);
		if(!que.empty())
		{
			ans+=que.top();	
			que.pop();
		}	
	}
	cout<<ans<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值