【动态规划】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.mfbz.cn/a/341815.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

在linux部署Prometheus+Grafana+Exporter监控系统性能

Prometheus、Grafana和Report组件是什么&#xff1f; Prometheus、Grafana和Exporter是常用于系统监控和指标收集的组合。 Prometheus是一种开源的系统监控和警报工具。它可以收集各种指标数据&#xff0c;并提供强大的查询语言和灵活的警报规则&#xff0c;用于实时监控系统…

[框架系列]-[通用lock框架]集成及具体配置使用

目录 一&#xff1a;框架集成 1.添加pom依赖 2.开启lock配置 二&#xff1a;配置详细介绍 1.配置清单 2.具体配置介绍 &#xff08;1&#xff09;implementer &#xff08;2&#xff09;type &#xff08;3&#xff09;transactionStrategy &#xff08;4&#xff09…

利用大模型审合同真的可以吗?

#大模型 #大模型审合同 最近有很多朋友留言&#xff0c;询问关于大模型审合同的问题&#xff0c;今天就小智一起来探讨下这个问题。 &#xff08;智合同-采用深度学习、自然语言处理技术、知识图谱等人工智能技术&#xff0c;为企业提供专业的合同相关的智能服务。其服务包含…

【Java IO】设计模式 (装饰者模式)

Java I/O 使用了装饰者模式来实现。 装饰者模式 请参考装饰者模式详解 装饰者(Decorator)和具体组件(ConcreteComponent)都继承自组件(Component)&#xff0c;具体组件的方法实现不需要依赖于其它对象&#xff0c;而装饰者组合了一个组件&#xff0c;这样它可以装饰其它装饰者…

Excel数据检索省力小工具(文末附源码)

Excel数据检索省力小工具&#xff08;文末附源码&#xff09; 引言 ​ 相信很多人都是用过VLOOKUP函数来检索和处理Excel数据。比如教师查看班级学生成绩表&#xff0c;想单独检索某个科目、某个学生&#xff0c;某一分数段&#xff08;80~90分数段内的成绩&#xff09;&…

网络安全基础概念

目录 网络安全背景 网络空间安全 --- Cyberspace 常见的网络安全术语 协议栈自身的脆弱性&#xff1a; 常见安全风险&#xff1a; 物理层--物理攻击 物理设备窃听&#xff1a; 链路层-- MAC洪泛攻击&#xff1a; 链路层--ARP欺骗 网络层--ICMP攻击 传输层--TCP SYN Flood攻击: …

信息检索与数据挖掘 | (八)语言建模的IR

文章目录 &#x1f4da;语言生成模型&#x1f4da;平滑&#x1f407;线性插值平滑方法(Lelinek-Mercer)&#x1f407;dirichlet 平滑&#x1f407;Vector space&#xff08;向量空间&#xff09; vs BM25 vs LM &#x1f4da;语言生成模型 传统的语言生成模型可以用于识别或生成…

南京观海微电子---时序分析基本概念(二)——保持时间

1. 概念的理解 以上升沿锁存为例&#xff0c;保持时间&#xff08;Th&#xff09;是指在触发器的时钟信号上升沿到来以后&#xff0c;数据稳定不变的时间。如下图所示&#xff0c;一个数据要在上升沿被锁存&#xff0c;那么这个数据需要在时钟上升沿到来后的保持时间内保持稳定…

展会日记:ICCAD2023,Samtec连接器无处不在

【序言】 “作为重要的电子元器件&#xff0c;连接器在如今的数字与现实世界中&#xff0c;扮演了不可或缺的角色。Samtec作为全球知名的连接器厂商&#xff0c;在芯片到板、板到板、射频、光模块等领域都有着卓越表现~ 今年&#xff0c;我们更是将这种存在感在2023 ICCAD上&a…

Nginx 基础使用

目录结构 进入Nginx的主目录我们可以看到这些文件夹 client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp其中这几个文件夹在刚安装后是没有的&#xff0c;主要用来存放运行过程中的临时文件 client_body_temp fastcgi_temp proxy_temp scg…

uniapp中打包Andiord app,在真机调试时地图以及定位功能可以正常使用,打包成app后失效问题(高德地图)

踩坑uniapp中打包Andiord app&#xff0c;在真机调试时地图以及定位功能可以正常使用&#xff0c;打包成app后失效问题_uniapp真机调试高德地图正常 打包apk高德地图就不加载-CSDN博客 问题&#xff1a; 目前两个项目&#xff0c;一个项目是从另一个项目里面分割出来的一整套…

华为云磁盘性能指标(参考)

MD[华为云磁盘性能指标(参考)] 云硬盘&#xff08;Elastic Volume Service, EVS&#xff09; 根据性能&#xff0c;磁盘可分为极速型SSD V2、极速型SSD、通用型SSD V2、超高IO、通用型SSD、高IO、普通IO。 性能指标(参考)&#xff0c;测速说明&#xff1a;操作系统-windows …

共襄Agent智能体盛举,实在智能2024生态伙伴大会杭州站圆满收官!

1月19日&#xff0c;以“实在Agent智能体”为主题的「2024实在智能生态伙伴大会&#xff08;杭州站&#xff09;」在杭州人工智能小镇隆重启幕&#xff01; 中国电信/联通/中海油等数十家央企子公司领导代表、天翼数科/华为/浪潮/统信/贝锐/vivo集团/新华三/中软国际/中投创展/…

华为AC+FIT AP组网配置

AC配置 vlan batch 100 to 101dhcp enableip pool apgateway-list 192.168.100.254 network 192.168.100.0 mask 255.255.255.0 interface Vlanif100ip address 192.168.100.254 255.255.255.0dhcp select globalinterface GigabitEthernet0/0/1port link-type trunkport trun…

Flutter 自定义AppBar实现滚动渐变

1、使用ListView实现上下滚动。 2、使用Stack&#xff1a;允许将其子部件放在彼此的顶部&#xff0c;第一个子部件将放置在底部。所以AppBar&#xff0c;写在ListView下面。 3、MediaQuery.removePadding&#xff1a;当使用ListView的时候发现&#xff0c;顶部有块默认的Padd…

【蓝桥杯冲冲冲】排队接水--贪心算法巩固 (≧∇≦)

蓝桥杯备赛 | 洛谷做题打卡day15 文章目录 蓝桥杯备赛 | 洛谷做题打卡day15排队接水题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 提示思路 题解代码我的一些话 排队接水 题目描述 有 n n n 个人在一个水龙头前排队接水&#xff0c;假如每个人接水的时间为 T i T_…

使用torch实现RNN

在实验室的项目遇到了困难&#xff0c;弄不明白LSTM的原理。到网上搜索&#xff0c;发现LSTM是RNN的变种&#xff0c;那就从RNN开始学吧。 带隐藏状态的RNN可以用下面两个公式来表示&#xff1a; 可以看出&#xff0c;一个RNN的参数有W_xh&#xff0c;W_hh&#xff0c;b_h&am…

Linux如何将文件或目录打成rpm包? -- fpm打包详解

&#x1f468;‍&#x1f393;博主简介 &#x1f3c5;云计算领域优质创作者   &#x1f3c5;华为云开发者社区专家博主   &#x1f3c5;阿里云开发者社区专家博主 &#x1f48a;交流社区&#xff1a;运维交流社区 欢迎大家的加入&#xff01; &#x1f40b; 希望大家多多支…

关于大模型学习中遇到的4

来源&#xff1a;网络 相关学习可查看文章&#xff1a;Transformer and Pretrain Language Models3-4​​​​​​​ 什么是MLP? MLP是多层感知器&#xff08;Multilayer Perceptron&#xff09;的缩写&#xff0c; 多层感知机&#xff08;MLP&#xff09;是一种人工神经网…

Tensorflow2.0笔记 - tensor的合并和分割

主要记录concat,stack,unstack和split相关操作的作用 import tensorflow as tf import numpy as nptf.__version__#concat对某个维度进行连接 #假设下面的tensor0和tensor1分别表示4个班级35名同学的8门成绩和两个班级35个同学8门成绩 tensor0 tf.ones([4,35,8]) tensor1 tf…
最新文章