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

作者推荐

【动态规划】【广度优先搜索】【状态压缩】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的盈利计划数。

动态规划的转移方程

前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 ] 完成本任务 \begin{cases} dp[preCount][p] += pre[preCount][p] & 不完成本任务 \\ 人数不足无法完成当前任务 & preCount+group[i] >n \\ dp[preCount+group[i]][min(minProfit,p+profit[i])] +=pre[preCount][p] & 完成本任务 \\ \end{cases} dp[preCount][p]+=pre[preCount][p]人数不足无法完成当前任务dp[preCount+group[i]][min(minProfit,p+profit[i])]+=pre[preCount][p]不完成本任务preCount+group[i]>n完成本任务

动态规划的初始值

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()1v[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();}
};

测试用例


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);}}

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++**实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/424363.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

V-bind缩写、V-on缩写、V-if、V-show、V-for、Computed计算属性、methods属性、监听属性watch实例

V-bind、V-for缩写 V-model V-if V-show V-for Computed计算属性 声明了一个计算属性 reversedMessage 。 提供的函数将用作属性 vm.reversedMessage 的 getter 。 vm.reversedMessage 依赖于 vm.message&#xff0c;在 vm.message 发生改变时&#xff0c;vm.reversedMessage…

性能优化(CPU优化技术)-NEON指令介绍

「发表于知乎专栏《移动端算法优化》」 本文主要介绍了 NEON 指令相关的知识&#xff0c;首先通过讲解 arm 指令集的分类&#xff0c;NEON寄存器的类型&#xff0c;树立基本概念。然后进一步梳理了 NEON 汇编以及 intrinsics 指令的格式。最后结合指令的分类&#xff0c;使用例…

如何在容器内部进行抓包

//先获取POD 的容器ID号 //去pod容器所在节点进行解析id为pid号 //通过pid号进入这个容器的网络命名空间 docker inspect --format {{.State.Pid}} 05f38d2a61e29b5a9d24fc7a3906991ab92ecd58ff7e0eb4e339a4cc6b2c4fc4 //访问容器内部&#xff0c;Node01节点

MATLAB初始化智能算法编码-产生随机不重复整数序列矩阵

产生随机不重复整数序列矩阵是智能算法最常用的操作之一,以下给出具体方法: clc;close all;clear all;warning off;%清除变量 rand(seed, 100); randn(seed, 100); format long g; N10; % 设定优化问题维数 lb0*ones(1,N);% 自变量上限 ub1*ones(1,N);% 自变量下限 popsize10…

​在 Linux ​中管理用户

在 Linux 系统中&#xff0c;用户是系统资源的主要使用者&#xff0c;每个用户都有一个唯一的标识符&#xff08;用户ID&#xff09;。为了更好地组织和管理用户&#xff0c;Linux 还引入了用户组的概念。用户组是用户的集合&#xff0c;有助于更有效地分配权限和资源。 用户是…

mysql 导入数据 1273 - Unknown collation: ‘utf8mb4_0900_ai_ci‘

前言: mysql 导入数据 遇到这个错误 1273 - Unknown collation: utf8mb4_0900_ai_ci 具体原因没有深究 但应该是设计数据库的 字符集类型会出现这个问题 例如: char varchar text..... utf8mb4 类型可以存储表情 在现在这个时代会用很多 以后会用的更多 所以不建议改…

react 实现页面状态缓存(keep-alive)

前言&#xff1a; 因为 react、vue都是单页面应用&#xff0c;路由跳转时&#xff0c;就会销毁上一个页面的组件。但是有些项目不想被销毁&#xff0c;想保存状态。 比如&#xff1a;h5项目跳转其他页面返回时&#xff0c;页面状态不丢失。设想一个 页面我滑倒了中间&#xf…

Java实现桃花峪滑雪场租赁系统 JAVA+Vue+SpringBoot+MySQL

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 游客服务2.2 雪场管理 三、数据库设计3.1 教练表3.2 教练聘请表3.3 押金规则表3.4 器材表3.5 滑雪场表3.7 售票表3.8 器材损坏表 四、系统展示五、核心代码5.1 查询教练5.2 教练聘请5.3 查询滑雪场5.4 滑雪场预定5.5 新…

AI创作之旅:探索提示工程的奇妙世界

&#x1f482; 个人网站:【 海拥】【神级代码资源网站】【办公神器】&#x1f91f; 基于Web端打造的&#xff1a;&#x1f449;轻量化工具创作平台&#x1f485; 想寻找共同学习交流的小伙伴&#xff0c;请点击【全栈技术交流群】 在当今信息爆炸的时代&#xff0c;人工智能的发…

人工智能原理实验4(2)——贝叶斯、决策求解汽车评估数据集

&#x1f9e1;&#x1f9e1;实验内容&#x1f9e1;&#x1f9e1; 汽车数据集 车子具有 buying,maint,doors,persons,lug_boot and safety六种属性&#xff0c;而车子的好坏分为uncc,ucc,good and vgood四种。 &#x1f9e1;&#x1f9e1;贝叶斯求解&#x1f9e1;&#x1f9e1;…

苹果笔记本MacBook电脑怎么卸载软件?三种方法快速卸载软件

苹果笔记本MacBook电脑是一款非常流行的电脑&#xff0c;但是有时候我们可能需要卸载一些不需要的软件。下面是一些简单的步骤&#xff0c;可以帮助您在MacBook电脑上卸载软件。 苹果笔记本MacBook电脑怎么卸载软件&#xff1f;三种实用方法快速卸载软件&#xff01; 方法一&a…

opencv#30 线性滤波

均值滤波原理 均值滤波步骤 Step1:求和。 Step2:计算平均值。 所谓均值滤波&#xff0c;就是求平均值的意思。我们假设在一个3*3的范围内有一个图像&#xff0c;其中这个图像每一个像素可能含有噪声&#xff0c;也可能不含噪声&#xff0c;我们是不知道的&#xff0c;因此通…