首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐
2025年5月20日 星期二 10:05pm

【动态规划】879. 盈利计划

  • 25-02-22 04:41
  • 3748
  • 13378
blog.csdn.net

作者推荐

【动态规划】【广度优先搜索】【状态压缩】847 访问所有节点的最短路径

本文涉及知识点

动态规划汇总

LeetCode879. 盈利计划

集团里有 n 名员工,他们可以完成各种各样的工作创造利润。
第 i 种工作会产生 profit[i] 的利润,它要求 group[i] 名成员共同参与。如果成员参与了其中一项工作,就不能参与另一项工作。
工作的任何至少产生 minProfit 利润的子集称为 盈利计划 。并且工作的成员总数最多为 n 。
有多少种计划可以选择?因为答案很大,所以 返回结果模 10^9 + 7 的值。
示例 1:
输入:n = 5, minProfit = 3, group = [2,2], profit = [2,3]
输出:2
解释:至少产生 3 的利润,该集团可以完成工作 0 和工作 1 ,或仅完成工作 1 。
总的来说,有两种计划。
示例 2:
输入:n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
输出:7
解释:至少产生 5 的利润,只要完成其中一种工作就行,所以该集团可以完成任何工作。
有 7 种可能的计划:(0),(1),(2),(0,1),(0,2),(1,2),以及 (0,1,2) 。
参数:
1 <= n <= 100
0 <= minProfit <= 100
1 <= group.length <= 100
1 <= group[i] <= 100
profit.length == group.length
0 <= profit[i] <= 100

动态规划

动态规划的状态表示

pre[j][k]表示 从前i个工作中,完成若干任务,出动了j人,利润为k的盈利计划数。例外:k==minProfit 时,包括利润大于minProfit的盈利计划数。
状态数:O(n × \times × minProfit )

动态规划的转移方程

前i个任务,出动 preCount 人,利润为p
{ d p [ p r e C o u n t ] [ p ] + = p r e [ p r e C o u n t ] [ p ] 不完成本任务 人数不足无法完成当前任务 p r e C o u n t + g r o u p [ i ] > n d p [ p r e C o u n t + g r o u p [ i ] ] [ m i n ( m i n P r o f i t , p + p r o f i t [ i ] ) ] + = p r e [ p r e C o u n t ] [ p ] 完成本任务 {dp[preCount][p]+=pre[preCount][p]不完成本任务人数不足无法完成当前任务preCount+group[i]>ndp[preCount+group[i]][min(minProfit,p+profit[i])]+=pre[preCount][p]完成本任务

⎧⎩⎨dp[preCount][p]+=pre[preCount][p]人数不足无法完成当前任务dp[preCount+group[i]][min(minProfit,p+profit[i])]+=pre[preCount][p]不完成本任务preCount+group[i]>n完成本任务
⎩ ⎨ ⎧​dp[preCount][p]+=pre[preCount][p]人数不足无法完成当前任务dp[preCount+group[i]][min(minProfit,p+profit[i])]+=pre[preCount][p]​不完成本任务preCount+group[i]>n完成本任务​
转移的方程的时间复杂度O(1),故总时间复杂度O(n × \times × minProfit )

动态规划的初始值

pre[0][0]=1

动态规划的填表顺序

i从小到大

动态规划的返回值

∑ i : 0 p r e . s i z e ( ) − 1 \sum\Large_{i:0 }^{pre.size()-1} ∑i:0pre.size()−1​v[i].back()

代码

核心代码

template<int MOD = 1000000007>
class C1097Int
{
public:
	C1097Int(long long llData = 0) :m_iData(llData% MOD)
	{

	}
	C1097Int  operator+(const C1097Int& o)const
	{
		return C1097Int(((long long)m_iData + o.m_iData) % MOD);
	}
	C1097Int& operator+=(const C1097Int& o)
	{
		m_iData = ((long long)m_iData + o.m_iData) % MOD;
		return *this;
	}
	C1097Int& operator-=(const C1097Int& o)
	{
		m_iData = (m_iData + MOD - o.m_iData) % MOD;
		return *this;
	}
	C1097Int  operator-(const C1097Int& o)
	{
		return C1097Int((m_iData + MOD - o.m_iData) % MOD);
	}
	C1097Int  operator*(const C1097Int& o)const
	{
		return((long long)m_iData * o.m_iData) % MOD;
	}
	C1097Int& operator*=(const C1097Int& o)
	{
		m_iData = ((long long)m_iData * o.m_iData) % MOD;
		return *this;
	}
	bool operator<(const C1097Int& o)const
	{
		return m_iData < o.m_iData;
	}
	C1097Int pow(long long n)const
	{
		C1097Int iRet = 1, iCur = *this;
		while (n)
		{
			if (n & 1)
			{
				iRet *= iCur;
			}
			iCur *= iCur;
			n >>= 1;
		}
		return iRet;
	}
	C1097Int PowNegative1()const
	{
		return pow(MOD - 2);
	}
	int ToInt()const
	{
		return m_iData;
	}
private:
	int m_iData = 0;;
};

class Solution {
public:
	int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit) {
		vector<vector<C1097Int<>>> pre(n + 1, vector<C1097Int<>>(minProfit + 1));
		pre[0][0] = 1;
		for (int i = 0; i < group.size(); i++)
		{
			auto dp = pre;//不完成当前任务
			for (int preCount = 0; preCount < n; preCount++)
			{
				for (int p = 0; p <= minProfit; p++)
				{
					const int iNewCount = preCount + group[i];
					if (iNewCount > n)
					{
						continue;
					}
					const int iNewProfit = min(minProfit, p + profit[i]);
					dp[iNewCount][iNewProfit] += pre[preCount][p];
				}
			}
			pre.swap(dp);
		}
		C1097Int<> biRet;
		for (const auto& v : pre)
		{
			biRet += v.back();
		}
		return biRet.ToInt();
	}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96

测试用例


template<class T>
void Assert(const T& t1, const T& t2)
{
	assert(t1 == t2);
}

template<class T>
void Assert(const vector<T>& v1, const vector<T>& v2)
{
	if (v1.size() != v2.size())
	{
		assert(false);
		return;
	}
	for (int i = 0; i < v1.size(); i++)
	{
		Assert(v1[i], v2[i]);
	}

}

int main()
{	
	int n,  minProfit;
	vector<int> group, profit;
	{
		Solution sln;
		n = 5, minProfit = 3, group = { 2, 2 }, profit = { 2, 3 };
		auto res = sln.profitableSchemes(n, minProfit, group, profit);
		Assert(res, 2);
	}
	{
		Solution sln;
		n = 10, minProfit = 5, group = { 2, 3, 5 }, profit = { 6, 7, 8 };
		auto res = sln.profitableSchemes(n, minProfit, group, profit);
		Assert(res, 7);
	}


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

2023年 1月第一版

class CBigMath
{
public:
static void AddAssignment(int* dst, const int& iSrc)
{
*dst = (dst + iSrc) % s_iMod;
}
static void AddAssignment(int
dst, const int& iSrc, const int& iSrc1)
{
*dst = (*dst + iSrc) % s_iMod;
*dst = (dst + iSrc1) % s_iMod;
}
static void AddAssignment(int
dst, const int& iSrc, const int& iSrc1, const int& iSrc2)
{
*dst = (*dst + iSrc) % s_iMod;
*dst = (*dst + iSrc1) % s_iMod;
*dst = (dst + iSrc2) % s_iMod;
}
static void SubAssignment(int
dst, const int& iSrc)
{
*dst = (s_iMod - iSrc + *dst) % s_iMod;
}
static int Add(const int& iAdd1, const int& iAdd2)
{
return (iAdd1 + iAdd2) % s_iMod;
}
static int Mul(const int& i1, const int& i2)
{
return((long long)i1 i2) % s_iMod;
}
private:
static const int s_iMod = 1000000007;
};
class Solution {
public:
int profitableSchemes(int n, int minProfit, vector& group, vector& profit) {
m_minProfit = minProfit;
vector pre((n + 1)
(minProfit + 1));
pre[0] = 1;
int iMaxN = 0;
int iMaxProfit = 0;
for (int i = 0; i < group.size(); i++)
{
vector dp = pre;
for (int j = 0; j <= iMaxN; j++)
{
for (int k = 0; k <= iMaxProfit; k++)
{
const int iNewN = j + group[i];
if (iNewN > n)
{
//员工不足
continue;
}
const int iNewProfit = min(minProfit, k + profit[i]);
CBigMath::AddAssignment(&dp[GetIndex(iNewN, iNewProfit)], pre[GetIndex(j, k)]);
}
}
pre.swap(dp);
iMaxN = min(n, iMaxN + group[i]);
iMaxProfit = min(minProfit, iMaxProfit + profit[i]);
}
int iNum = 0;
for (int i = 0; i <= iMaxN; i++)
{
CBigMath::AddAssignment(&iNum ,pre[GetIndex(i, minProfit)]);
}
return iNum;
}
inline int GetIndex(int n, int pro)
{
return n *(m_minProfit + 1) + pro;
}
int m_minProfit;
};

2023年1月版

class CBigMath
{
public:
static void AddAssignment(int* dst, const int& iSrc)
{
*dst = (dst + iSrc) % s_iMod;
}
static void AddAssignment(int
dst, const int& iSrc, const int& iSrc1)
{
*dst = (*dst + iSrc) % s_iMod;
*dst = (dst + iSrc1) % s_iMod;
}
static void AddAssignment(int
dst, const int& iSrc, const int& iSrc1, const int& iSrc2)
{
*dst = (*dst + iSrc) % s_iMod;
*dst = (*dst + iSrc1) % s_iMod;
*dst = (dst + iSrc2) % s_iMod;
}
static void SubAssignment(int
dst, const int& iSrc)
{
*dst = (s_iMod - iSrc + *dst) % s_iMod;
}
static int Add(const int& iAdd1, const int& iAdd2)
{
return (iAdd1 + iAdd2) % s_iMod;
}
static int Mul(const int& i1, const int& i2)
{
return((long long)i1 i2) % s_iMod;
}
private:
static const int s_iMod = 1000000007;
};
class Solution {
public:
int profitableSchemes(int n, int minProfit, vector& group, vector& profit) {
m_minProfit = minProfit;
vector pre((n + 1)
(minProfit + 1));
pre[0] = 1;
int iMaxN = 0;
int iMaxProfit = 0;
for (int i = 0; i < group.size(); i++)
{
vector dp = pre;
for (int j = 0; j <= iMaxN; j++)
{
for (int k = 0; k <= iMaxProfit; k++)
{
const int iNewN = j + group[i];
if (iNewN > n)
{
//员工不足
continue;
}
const int iNewProfit = min(minProfit, k + profit[i]);
CBigMath::AddAssignment(&dp[GetIndex(iNewN, iNewProfit)], pre[GetIndex(j, k)]);
}
}
pre.swap(dp);
iMaxN = min(n, iMaxN + group[i]);
iMaxProfit = min(minProfit, iMaxProfit + profit[i]);
}
int iNum = 0;
for (int i = 0; i <= iMaxN; i++)
{
CBigMath::AddAssignment(&iNum ,pre[GetIndex(i, minProfit)]);
}
return iNum;
}
inline int GetIndex(int n, int pro)
{
return n *(m_minProfit + 1) + pro;
}
int m_minProfit;
};

扩展阅读

视频课程

有效学习:明确的目标 及时的反馈 拉伸区(难度合适),可以先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快

速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

相关

下载

想高屋建瓴的学习算法,请下载《喜缺全书算法册》doc版
https://download.csdn.net/download/he_zhidan/88348653

我想对大家说的话
闻缺陷则喜是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 **C+

+17**
如无特殊说明,本算法用**C++**实现。

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览61211 人正在系统学习中
群中有博文配套源码
QQ群名片
注:本文转载自blog.csdn.net的闻缺陷则喜何志丹的文章"https://blog.csdn.net/he_zhidan/article/details/135693564"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top