《dp補卡——多重背包》

多重背包簡介:
有N種物品和一個容量為V的背包。第i種物品最多有Mi件可用,每件耗費的空間為Ci,價值為Wi。求解將哪些物品裝入背包可使得這些物品耗費的空間總和不超過背包容量,且價值總和最大。
將Mi件攤開,就是一個01背包問題。
如下列兩表就是等價的,圖來源于代碼隨想錄。
在這里插入圖片描述

void test_multi_pack() 
{vector<int> weight = {1,3,4};vector<int> value = {15,20,30};vector<int> nums = {2,3,2};int bagWeight = 10;//進行展開,轉化為01背包問題for(int i = 0; i < nums.size(); i++){while(nums[i] > 1)	{//nums[i]保留到1,把其他的多余的個數展開weight.push_back(weight[i]);value.push_back(value[i]);nums[i]--;}}vector<int> dp(bagWeight + 1, 0);for(int i = 0; i < weight.size(); i++)	//遍歷物品{for(int j = bagWeight; j >= weight[i]; j--)	//遍歷背包容量{dp[j] = max(dp[j],dp[j-weight[i]] + values[i])}}cout << dp[bagWeight] << endl;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377098.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377098.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377098.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

kafka消息確認ack_什么是確認(ACK)? ACK代表什么?

kafka消息確認ackACK&#xff1a;致謝 (ACK: Acknowledgment) An acknowledgment (ACK) is a signal that is passed among the communicating processes, computers, or devices to indicate acknowledgment, or delivery of the message, as a component of a communications…

CocoaAsyncSocket 套接字

CocoaAsyncSocket 套接字 https://github.com/robbiehanson/CocoaAsyncSocket Asynchronous socket networking library for Mac and iOS 用于iOS以及Mac的異步套接字網絡庫。 TCP GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries. Here are the key…

谷歌瀏覽器設置緩存方法

谷歌瀏覽器設置緩存方法&#xff1a; 1、在桌面Google Chrome快捷方式&#xff0c;目標&#xff1a;找到 C:\Users\Splendid\AppData\Local\…\Application\chrome.exe 在這后面加上-Disk-Cache-Dir”Z:\TEMP” 注意: -Disk前面有空格&#xff0c;”Z:\TEMP” 是文件存放在Z盤T…

《dp補卡——買賣股票問題》

目錄121. 買賣股票的最佳時機貪心dp思路滾動數組優化122. 買賣股票的最佳時機 II123. 買賣股票的最佳時機 III188. 買賣股票的最佳時機 IV309. 最佳買賣股票時機含冷凍期714. 買賣股票的最佳時機含手續費121. 買賣股票的最佳時機 貪心 取最左最小值&#xff0c;取最右最大值&…

oo0ooo0ooo0oo_OoO的完整形式是什么?

oo0ooo0ooo0ooOoO&#xff1a;外出 (OoO: Out of Office) OoO is an abbreviation of "Out of Office". OoO是“不在辦公室”的縮寫。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email…

SP2010開發和VS2010專家食譜--第三章節--高級工作流(2)--為沙盒解決方案創建自定義活動...

盡管沙河解決方案功能有限&#xff0c;你仍然可以開發自定義活動&#xff0c;在SharePoint Designer中使用而不用改變web.config或添加.ACTION文件到根文件夾。 轉載于:https://www.cnblogs.com/crazygolf/p/3856795.html

sql where 1=1和 0=1 的作用

where 11; 這個條件始終為True&#xff0c;在不定數量查詢條件情況下&#xff0c;11可以很方便的規范語句。 一、不用where 11 在多條件查詢中的困擾 舉個例子&#xff0c;如果您做查詢頁面&#xff0c;并且&#xff0c;可查詢的選項有多個&#xff0c;同時&#xff0c;還讓用戶…

j@2ff4f00f_J4F的完整形式是什么?

j2ff4f00fJ4F&#xff1a;只是為了好玩 (J4F: Just For Fun) J4F is an abbreviation of "Just For Fun". J4F是“ Just For Fun”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Faceboo…

《dp補卡——子序列問題》

目錄300. 最長遞增子序列674. 最長連續遞增序列718. 最長重復子數組1143. 最長公共子序列53. 最大子序和392. 判斷子序列115. 不同的子序列583. 兩個字符串的刪除操作72. 編輯距離647. 回文子串 &#xff08;與 5.最長回文子串思路差不多&#xff09;516. 最長回文子序列300. 最…

[LeetCode] Maximal Rectangle

Given a 2D binary matrix filled with 0s and 1s, find the largest rectangle containing all ones and return its area. 在做 Largest Rectangle in Histogram的時候有人說可以用在這題&#xff0c;看了一下還真是&#xff0c;以每行為x軸&#xff0c;每列往上累計的連續的…

什么是alpha測試_什么是ALPHA?

什么是alpha測試Α (ALPHA) Alpha is the first and foremost letter of the Greek alphabet. In the classification of Greek numerals or numbers, it constitutes a value of 1. Alpha是希臘字母的第一個也是最重要的字母 。 在希臘數字或希臘數字的分類中&#xff0c;它的…

《leetcode : 647. 回文子串 思考分析雙指針解法》

647. 回文子串 如何確定是回文串&#xff1a; 找中心然后往兩邊擴散&#xff0c;判斷是否對稱即可。 在遍歷中心點的時候&#xff0c;注意中心點可以是一個元素也可以是兩個元素。 class Solution { public:int cal_two_extend(const string& s,int i,int j,int n){int re…

天草初級班(3)

算術運算指令算術運算指令是反映CPU計算能力的一組指令&#xff0c;也是編程時經常使用的一組指令。它包括&#xff1a;加、減、乘、除及其相關的輔助指令。 該組指令的操作數可以是8位、16位和32位(80386)。當存儲單元是該類指令的操作數時&#xff0c;該操作數的尋址方式可以…

4.3.3版本之引擎bug

bug描述&#xff1a;   IOS設備上&#xff0c;當使用WWW www WWW.LoadFromCacheOrDownload(url, verNum); 下載資源時&#xff0c;第一次下載某個資源&#xff0c;www.assetBundle必定為空。 解決辦法&#xff1a;   引擎版本降到4.3.2或者升到4.3.4或更高。 這個bug絕對是…

sml完整形式_411的完整形式是什么?

sml完整形式411&#xff1a;信息 (411: Information) 411 is an abbreviation of “Information". 411是“信息”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenger, a…

php 檢測用戶是否關閉瀏覽器

1、例子1 echo str_repeat(" ",3000);ignore_user_abort(true); mylog(online);while (true) {/** 1、程序正常結束 connection_status 0* 2、點擊瀏覽器“停止”按鈕 connection_status 1* 3、超時 connection_status 2*/echo "test<br>\n&qu…

explain用法

explain用法 EXPLAIN SELECT …… 變體&#xff1a; 1. EXPLAIN EXTENDED SELECT …… 將執行計劃“反編譯”成SELECT語句&#xff0c;運行SHOW WARNINGS 可得到被MySQL優化器優化后的查詢語句 2. EXPLAIN PARTITIONS SELECT …… 用于分區表的EXPLAIN 執行計劃包含的信息 id…

《位運算技巧以及Leetcode的一些位運算題目》

目錄技巧練習位運算[461. 漢明距離](https://leetcode-cn.com/problems/hamming-distance/)[190. 顛倒二進制位](https://leetcode-cn.com/problems/reverse-bits/)[136. 只出現一次的數字](https://leetcode-cn.com/problems/single-number/)[260. 只出現一次的數字 III](http…

linux讀取配置文件(C語言版)

一個通用的linux系統中C語言版讀取配置文件的函數。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <errno.h>#define KEYVALLEN 100/* 刪除左邊的空格 */ char * l_trim(char * szOutput, con…

java 范圍搜尋要怎么弄_搜索范圍

java 范圍搜尋要怎么弄Problem statement: 問題陳述&#xff1a; Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. 給定一個以升序排列的整數nums數組&#xff0c;請找到給定目標值的開始和結束…