左神算法学习日记——堆(二)

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<time.h>
#include<queue>
#include<algorithm>
#include<xfunctional>
using namespace std;

class project
{
public:
	int cost;
	int profit;
	project() = default;
	project(int c, int p)
	{
		cost = c;
		profit = p;
	}
	project(const project& p)//push进堆时用到了拷贝构造函数,虽然不写也无所谓
	{
		cost = p.cost;
		profit = p.profit;
	}
	project& operator = (const project& p)
	{
        if(this!=&p)
		{cost = p.cost;
		profit = p.profit;}
        return *this;
	}
};

struct co//以cost为基准建立小根堆
{
	bool operator()(project& _Left, project& _Right)
	{
		return (_Left.cost > _Right.cost);
	}
};

struct _pr//以cost为基准建立大根堆
{
	bool operator()(project& _Left, project& _Right)
	{
		return (_Left.profit < _Right.profit);
	}
};

int main()
{
	vector<int> costs = { 1, 2, 5 };
	vector<int> profits = { 4, 3, 7 };
	int k = 2, m = 3;
	vector<project> pro;
	for (int i = 0; i < costs.size(); i++)
	{
		pro.push_back(project(costs[i], profits[i]));
	}
	priority_queue<project, vector<project>, co> small;
	priority_queue<project, vector<project>, _pr> big;//贪心策略,可以随时随地push并得到最大
                                                           //值
	for (int i = 0; i < pro.size(); i++)
	{
		small.push(pro[i]);
	}
	while (k>0)
	{
		while (!small.empty() && small.top().cost < m)
		{
			big.push(small.top());
			small.pop();
		}
		m += big.top().profit;
		big.pop();
		k--;
	}
	cout << m;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值