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];
    }
};
相关推荐
jerryinwuhan2 分钟前
《Python数据预处理》第四章
开发语言·python
长江&后浪5 分钟前
防火墙的描述
开发语言·网络·php
观无26 分钟前
若依EasyExcel实现单元格合并
开发语言·前端·javascript
雨晨源码(同名B站)28 分钟前
基于Python的网易云音乐评论数据情感化分析系统 音乐爬虫信息可视化 |SnowNLP评论情感分析
开发语言·hadoop·爬虫·python·信息可视化·毕业设计
间歇性努力持续性发呆的野生快乐选手31 分钟前
栈的性质(进栈,出栈,访问)
数据结构·c++
我不是疯子是傻子42 分钟前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆1 小时前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
旖旎夜光1 小时前
LeetCode 904:水果成篮(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
霸道流氓气质1 小时前
Java中信号量(Semaphore):从本地到分布式
java·开发语言·分布式
Patrick在香港2 小时前
Python正则表达式实战:解析和验证香港身份证、车牌、电话格式
开发语言·python·正则表达式