当前位置:网站首页>Multiple backpacks!
Multiple backpacks!
2022-07-23 14:58:00 【ATTACH_ Fine】
Yes N Items And a Capacity of V The backpack . The first i There are at most Mi Pieces available , The space consumed by each piece is Ci , The value is Wi . Find out which items can be loaded into the backpack to make the space consumed by these items The total does not exceed the capacity of the backpack , And The sum of value is the greatest .

It makes no difference , This turns into a 01 It's a knapsack problem , And only once per item .
Code
public void testMultiPack1(){
// Version of a : Change the number of items to 01 Knapsack format
List<Integer> weight = new ArrayList<>(Arrays.asList(1, 3, 4));
List<Integer> value = new ArrayList<>(Arrays.asList(15, 20, 30));
List<Integer> nums = new ArrayList<>(Arrays.asList(2, 3, 2));
int bagWeight = 10;
for (int i = 0; i < nums.size(); i++) {
while (nums.get(i) > 1) {
// Expand the item into i
weight.add(weight.get(i));
value.add(value.get(i));
nums.set(i, nums.get(i) - 1);
}
}
int[] dp = new int[bagWeight + 1];
for(int i = 0; i < weight.size(); i++) {
// Traverse the items
for(int j = bagWeight; j >= weight.get(i); j--) {
// Traverse the backpack capacity
dp[j] = Math.max(dp[j], dp[j - weight.get(i)] + value.get(i));
}
System.out.println(Arrays.toString(dp));
}
}
边栏推荐
- MySQL unique index has no duplicate value, and the error is repeated
- After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting
- 头部姿态估计原理及可视化_loveliuzz的博客-程序员宅基地_头部姿态估计
- 【无标题】
- Supervisor installation and use
- day18
- Kettle implements shared database connection and insert update component instances
- Linux scheduled database backup script
- The self-developed data products have been iterated for more than a year. Why not buy third-party commercial data platform products?
- Kettle實現共享數據庫連接及插入更新組件實例
猜你喜欢

Redis | 非常重要的中间件

OpenCV计算外包矩形

FastAPI应用加入Nacos

【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显

什麼是Per-Title編碼?

【测试平台开发】21. 完成发送接口请求显示响应头信息

Educational Codeforces Round 132 (Rated for Div. 2) D. Rorororobot

@FeignClient使用详细教程(图解)

Fastapi application joins Nacos

C language project practice: 24 point game calculator (based on knowledge points such as structure, pointer, function, array, loop, etc.)
随机推荐
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip
【刷题记录】19. 删除链表的倒数第 N 个结点
身份证号正则验证
直播课堂系统03-model类及实体
APtos 简介及机制
Introduction and mechanism of Aptos
自研的数据产品迭代了一年多,为什么不买第三方商业数据平台产品呢?
Postgresql快照优化Globalvis新体系分析(性能大幅增强)
It is suggested that Siyuan notes can be compatible with third-party sync disks
C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
[applet automation minium] III. element positioning - use of wxss selector
Due to resource constraints, the namenode fails to start with an error unable to create new native thread
【测试平台开发】21. 完成发送接口请求显示响应头信息
什么是Promise?Promise有什么好处
[software testing] how to sort out your testing business
[interview frequency] cookies, sessions, tokens? Don't worry about being asked after reading it
Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.
LZ77 file compression
leetcode: 17. 电话号码的字母组合
基于nextcloud构建个人网盘