C++ 完全背包时间优化、完全背包空间优化

完全背包时间优化:

状态转移方程:dp[i][j] = max(dp[i-1][j], dp[i][j - c[i]] + w[i])

有以下的内容,状态转移与k无关了,状态转移的时间复杂度从O(m)变成了O(1)

对于第i个物品,其实有两种选择,一个都不放,至少放一个。

一个都不放,就是前i-1种物品放满一个容量为j的背包,是dp[i-1][j]

至少放一个的话,我们尝试在"前i个物品放满一个容量为j的背包",拿到一个物品,即前i种物品放满一个容量为j-c[i]的背包,这个时候便是dp[i][j-c[i]] + w[i]

代码分析,核心代码

function KnapsackComplete(n, m, c[n+1], w[n+1], dp[n+1][m+1])

dp[0][0] = init

for i->(1, m)

dp[0][i] = inf

for i-> (1,n)

for j-> (0, c[i]-1)

dp[i][j] = dp[i-1][j]

for j->(c[i], m)

dp[i][j] = opt(dp[i-1][j], dp[i][j-c[i]]+w[i])

代码练习 1 对应蓝桥云课 小明的背包2 见下

cpp 复制代码
#include <iostream>
using namespace std;

#define maxn 1001
#define maxv 1001
#define inf -1
#define init 0
#define vType int

vType opt(vType a, vType b){
  if(a == inf) return b;
  if(b == inf) return a;
  return a>b ? a:b;
}
void KnapsackComplete(int n, int V, int w[maxn], vType v[maxv], vType dp[maxn][maxv]){
  for(int i=1; i<=V; ++i){
    dp[0][i] = inf;
  }
  dp[0][0] = init;

  for(int i=1; i<=n; ++i){
    for(int j=0; j<w[i]; ++j){
      dp[i][j] = dp[i-1][j];
    }
    for(int j=w[i]; j<=V; ++j){
      dp[i][j] = max(dp[i-1][j], dp[i][j-w[i]] + v[i]);
    }
  }
}

int n, V;
int w[maxn];
vType v[maxn];
vType dp[maxn][maxv];

int main()
{
  cin >> n >> V;
  for(int i=1; i<=n; ++i){
    cin >> w[i] >> v[i];
  }
  KnapsackComplete(n, V, w, v, dp);
  int ans = inf;
  for(int i=0; i <= V; ++i){
    ans = max(ans, dp[n][i]);
  }
  cout << ans << endl;
  // 请在此输入您的代码
  return 0;
}

完全背包空间优化,公式见下

dp[j] = max(dp[j], dp[j - c[i]] + w[i])

代码分析:

核心代码,

function KnapsackComplete(n, m, c[n+1], w[n+1], dp[m+1])

dp[0] = init

for i->(1, m)

dp[i] = inf

for i-> (1,n)

for j-> (c[i], m)

dp[j] = opt(dp[j], dp[j-c[i]]+w[i])

代码练习 1 对应蓝桥云课 小明的背包2 代码见下

cpp 复制代码
#include <iostream>
using namespace std;

#define maxn 1001
#define maxv 1001
#define inf -1
#define init 0
#define vType int

vType opt(vType a, vType b){
  if(a == inf) return b;
  if(b == inf) return a;
  return a>b ? a:b;
}
void KnapsackComplete(int n, int V, int w[maxn], vType v[maxv], vType dp[maxv]){
  dp[0] = init;

  for(int i=1; i<=V; ++i){
    dp[i] = inf;
  }
  for(int i=1; i<=n; ++i){
    for(int j=w[i]; j<=V; ++j){
      dp[j] = opt(dp[j], dp[j-w[i]]+v[i]);
    }
  }
}

int n, V;
int w[maxn];
vType v[maxn];
vType dp[maxv];

int main()
{
  cin >> n >> V;
  for(int i=1; i<=n; ++i){
    cin >> w[i] >> v[i];
  }
  KnapsackComplete(n, V, w, v, dp);
  int ans = inf;
  for(int i=0; i <= V; ++i){
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  // 请在此输入您的代码
  return 0;
}

代码练习2 对应力扣硬币 代码见下

cpp 复制代码
#define maxn 5
#define maxv 1000001
#define inf 0
#define init 1
#define vType int

vType opt(vType a, vType b){
  if(a == inf) return b;
  if(b == inf) return a;
  return (a+b)%1000000007;
}
void KnapsackComplete(int n, int V, int w[maxn], vType v[maxv], vType dp[maxv]){
  dp[0] = init;

  for(int i=1; i<=V; ++i){
    dp[i] = inf;
  }
  for(int i=1; i<=n; ++i){
    for(int j=w[i]; j<=V; ++j){
      dp[j] = opt(dp[j], dp[j-w[i]]+v[i]);
    }
  }
}

int n, V;
int w[maxn] = {-1, 25, 10, 5, 1};
vType v[maxn] = {-1, 0, 0, 0, 0};
vType dp[maxv];

class Solution {
public:
    int waysToChange(int n) {
        KnapsackComplete(4, n, w, v, dp);
        return dp[n];
    }
};
相关推荐
清铎14 小时前
项目_Agent实战
开发语言·人工智能·深度学习·算法·机器学习
BoJerry77714 小时前
数据结构——单链表(不带头)【C】
c语言·开发语言·数据结构
Queenie_Charlie14 小时前
位移运算
c++·位运算
hurrycry_小亦14 小时前
洛谷题目:P1365 WJMZBMR打osu! / Easy 题解(本题较简)
c++
m0_7487080514 小时前
C++代码移植性设计
开发语言·c++·算法
Dr.Kun14 小时前
【鲲码园PsychoPy】Go/No-go范式
开发语言·后端·golang
郝学胜-神的一滴14 小时前
Linux Socket模型创建流程详解
linux·服务器·开发语言·网络·c++·程序人生
王老师青少年编程14 小时前
2024信奥赛C++提高组csp-s复赛真题及题解:决斗
c++·真题·csp·信奥赛·csp-s·提高组·决斗
「QT(C++)开发工程师」14 小时前
C++ 观察者模式
java·c++·观察者模式
可问春风_ren14 小时前
Vue3 入门详解:从基础到实战
开发语言·前端·javascript·vue.js·前端框架·ecmascript·edge浏览器