当前位置: 首页 > news >正文

01背包,完全背包,分组背包,多重背包例题

一.01背包

416. 分割等和子集 - 力扣(LeetCode)

class Solution {
public:bool canPartition(vector<int>& nums) {int sum=0;for(int i=0;i<nums.size();i++){sum+=nums[i];}int k=0;if(sum%2!=0){return false;}k=sum/2;vector<int>dp(k+1,0);for(int i=0;i<nums.size();i++){for(int j=k;j>=0;j--){if(j>=nums[i]){dp[j]=max(dp[j],dp[j-nums[i]]+nums[i]);}}}if(dp[k]==k){return true;}return false;}
};

二.完全背包:

P1616 疯狂的采药 - 洛谷

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<iomanip>
#include<limits>
using namespace std;
#define int long long
void solve() {int n, m;cin >> n >> m;vector<int>times(m + 1), val(m + 1);for (int i = 0; i < m; i++) {cin >> times[i] >> val[i];}vector<int>dp(n + 1, 0);for (int i = 0; i < m; i++) {for (int j = 0; j <= n; j++) {if(j>=times[i])dp[j] = max(dp[j], dp[j - times[i]] + val[i]);}}cout << dp[n] << endl;}
signed main() {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int t;t = 1;while (t--)solve();return 0;
}

三.分组背包:

P1757 通天之分组背包 - 洛谷

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void solve() {vector<vector<int>>w(102, vector<int>(1002, 0));vector<vector<int>>v(102, vector<int>(1002, 0));int n, m;cin >> m>>n;int maxn = 0;for (int i = 0; i < n; i++) {int a, b, c;cin >> a >> b >> c;w[c].push_back(a);v[c].push_back(b);maxn = max(maxn, c);}vector<vector<int>>dp(maxn+1,vector<int>(m+1,0));for (int i = 1; i <= maxn; i++) {for (int j = 0; j<=m; j++) {for (int k = 0; k < w[i].size(); k++) {if (j >= w[i][k]) {dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i][k]] + v[i][k]);}}}}cout << dp[maxn][m] << endl;
}
signed main() {int t = 1;while (t--)solve();return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void solve() {vector<vector<int>>w(102, vector<int>(1002, 0));vector<vector<int>>v(102, vector<int>(1002, 0));int n, m;cin >> m >> n;int maxn = 0;for (int i = 0; i < n; i++) {int a, b, c;cin >> a >> b >> c;w[c].push_back(a);v[c].push_back(b);maxn = max(maxn, c);}vector<int>dp(m + 1, 0);for (int i = 1; i <= maxn; i++) {for (int j = m; j >= 0; j--) {for (int k = 0; k < w[i].size(); k++) {if (j >= w[i][k]) {dp[j] = max(dp[j], dp[j - w[i][k]] + v[i][k]);}}}}cout << dp[m] << endl;
}
signed main() {int t = 1;while (t--)solve();return 0;
}

四.多重背包:

4. 多重背包问题 I - AcWing题库

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void solve() {int n, m;cin >> n >> m;vector<int>v(n), w(n), s(n);for (int i = 0; i < n; i++) {cin >> v[i] >> w[i] >> s[i];}vector<int>dp(m + 1, 0);for (int i = 0; i < n; i++) {for (int j = m; j >= 0; j--) {for (int k = 1; k <= s[i]&&k*v[i]<=j; k++) {dp[j] = max(dp[j], dp[j - k * v[i]] + k * w[i]);}}}cout << dp[m] << endl;
}
signed main() {int t = 1;while (t--)solve();return 0;
}


文章转载自:

http://rBQZL3Qa.pLxhq.cn
http://jNPijZh3.pLxhq.cn
http://yKUNXR7B.pLxhq.cn
http://6aTmQzoV.pLxhq.cn
http://j5PkVbxG.pLxhq.cn
http://obaFy99U.pLxhq.cn
http://S6CVkEAt.pLxhq.cn
http://tGaC4Cik.pLxhq.cn
http://XDWOUTDk.pLxhq.cn
http://jVvipzqP.pLxhq.cn
http://EY1lk4da.pLxhq.cn
http://PVD6g3pi.pLxhq.cn
http://PUN13Eho.pLxhq.cn
http://HPaOTfMK.pLxhq.cn
http://zg8nBcQK.pLxhq.cn
http://zrvo7vvt.pLxhq.cn
http://y09sfnGo.pLxhq.cn
http://vlpxMuRN.pLxhq.cn
http://MS4JCeAx.pLxhq.cn
http://avgKPcwn.pLxhq.cn
http://vVcjXzoR.pLxhq.cn
http://Sk3jzmvu.pLxhq.cn
http://qAaubdil.pLxhq.cn
http://1SpGGhbZ.pLxhq.cn
http://eWXwFLrP.pLxhq.cn
http://qnQ0roFc.pLxhq.cn
http://zsGqBOUp.pLxhq.cn
http://MACxKlDz.pLxhq.cn
http://ErPE8YJb.pLxhq.cn
http://8oLMTlaN.pLxhq.cn
http://www.dtcms.com/a/375969.html

相关文章:

  • Ansible之playbook
  • MapReduce :Map阶段分区后,数据怎么找到Reducer?
  • 项目研发实录:电子称SDK封装dll给到QT和C#调用
  • 短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
  • 代码随想录算法训练营第三十五天|背包问题 二维 背包问题 一维 46. 携带研究材料 416. 分割等和子集
  • FTP文件传输服务
  • 代码随想录第七天|● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 18.四数之和
  • SAP R/3系统模块结构
  • leetcode 217 存在重复元素
  • 前端 Word 模板参入特定数据 并且下载
  • LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
  • 深度学习(五):过拟合、欠拟合与代价函数
  • 【JS】import.meta.env,process.env,window三种环境变量获取方式的区别
  • 交付只是起点:从“纸上蓝图”到“价值闭环”的保障实践,数字孪生保障落地的“三重防护网
  • LLM大模型-大模型 API 集成使用、部署本地大模型(huggingface、modelscope)、实现Qwen和Deepseek本地部署
  • Redis的入门与应用
  • pybind11错误书
  • 在 PostgreSQL中查看有哪些用户和用户权限
  • ctfshow- web入门-XXE漏洞
  • 六级第二关———坐地铁(1)
  • 实用 html 小工具
  • C#(链表创建与原地反转)
  • 光伏MPPT——拓扑结构及发波方式
  • Flink通讯超时问题深度解析:Akka AskTimeoutException解决方案
  • 美团核销接口助力第三方供应商拓展市场份额的策略
  • 基于dijkstra算法的WSN网络MAC协议matlab仿真,分析网络延迟与网络开销
  • 《Linux运维工程师基础技能测试简答题》
  • CPUID
  • aiagent知识点
  • DPO原理 | 公式推导