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

完全背包时间优化:

状态转移方程:dpij = max(dpi-1j, dpij - c\[i] + wi)

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

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

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

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

代码分析,核心代码

function KnapsackComplete(n, m, cn+1, wn+1, dpn+1m+1)

dp00 = init

for i->(1, m)

dp0i = inf

for i-> (1,n)

for j-> (0, ci-1)

dpij = dpi-1j

for j->(ci, m)

dpij = opt(dpi-1j, dpij-c\[i]+wi)

代码练习 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;
}

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

dpj = max(dpj, dpj - c\[i] + wi)

代码分析:

核心代码,

function KnapsackComplete(n, m, cn+1, wn+1, dpm+1)

dp0 = init

for i->(1, m)

dpi = inf

for i-> (1,n)

for j-> (ci, m)

dpj = opt(dpj, dpj-c\[i]+wi)

代码练习 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];
    }
};
相关推荐
用户8055336980316 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK1 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境2 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境2 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴3 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境5 天前
C++ 的Eigen 库全解析
c++
卷无止境5 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴5 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18007 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴7 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake